Hi the below is a snippet of my code
var wishList = App42API.buildStorageService() as? StorageService
var json: [String: AnyObject]!
init() {
wishList?.findAllDocuments(dataBase, collectionName: collectionName, completionBlock: {(success, response, exception) -> Void in
if(success){
let storage = response as! Storage
let jsonDocList = storage.jsonDocArray
var jsonString: String {
var someString: String = ""
for jsonDoc in jsonDocList{
someString = jsonDoc.jsonDoc
}
return someString
}
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do {
self.json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String: AnyObject]
}catch{
print(error)
}
print(self.json)
}else{
NSLog("%@", exception.reason! }})}})}
func printJSON(){
if json != nil{
print(json)
}else{
print("error") //Always return nil: error
}
}
The variable json only has value when it's in the if(success) statement. Even after it has been successfuly executed in the if(success) statement and I try to use the variable json elsewhere, it always returns nil.
Can you please help as of why this is the case? How do I keep the value in json outside the if(success) statement?