I am working with SwiftyJSON which is great. I am however having an issue with storing the JSON(data:) result in a property in my viewController. The standard use of SwiftyJSON works fine.
let json = JSON(data: data)
let name = json[1]["name"].string
My problem occurs when I try to create a property to store the result of JSON(data:)
// Property
var jsonData : JSON?
someMethod()
{
let json = JSON(data: data)
self.jsonData = json
if let name = self.jsonData[1]["name"].string
{
print(name)
}
}
When I do this I get an error on the following line.
if let name = self.jsonData[1]["name"].string
Cannot find member 'string'
Does anyone know why this is?
You are using an optional property.
var jsonData : JSON?
just use
in place of
in your case complier trying to find a property which can be a nil.