I though that the comments of the people explain very well what is your problems nevertheless I think that make a function to handle your request using closures and then handling in completion handler the rest can help you to organize your code, something like in the following way:
func getJSON(url: String, completionHandler: (json: JSON?, error: NSError?) -> ()) {
Alamofire.request(.GET, url)
.responseJSON { (req, res, data, error) in completionHandler(
json: {
if let d = data {
var parse = JSON(d)
return parse
}
return nil
}(), error: error)
}
}
And then you can call in your init
in the following way:
init(filename: String) {
self.getJSON("http://localhost:2403/users") { json, error in
if(error != nil) {
NSLog("Error: (error)")
}
else {
self.id = json["id"]
let filePath = NSURL(string: "http://localhost:2403/users")
let jsonData = NSData(contentsOfURL:filePath!)
let json = JSON(data: jsonData!, options: NSJSONReadingOptions.AllowFragments, error: nil)
for (key: String, subJson: JSON) in json {
var language:String?, link: String?, description:String?, greetingText: String?
for (key1, value:JSON) in subJson {
switch key1 {
case "displayName": language = value.string
case "id": link = value.string
case "username": description = value.string
case "mainSkill": greetingText = value.string
default: break
}
}
let greeting = Greeting(language: language, link: link, description: description, greetingText: greetingText)
self.greetings.append(greeting)
self.greetings = self.greetings.filter { $0.link != "(self.id)"} //this filter
println(self.id)
}
}
}
With the above code you make use of the closures to sure that your request is completed.
I hope this help you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…