2015-08-24 44 views
-1

我正在使用Alamofire來解析JSON數據。在下面提到的代碼中,除了'var id:JSON?'之外,一切工作都正常。雖然它的更新很好,但它在某個點(底部)保持返回零。我需要把這個變量放在過濾器關閉中。變量不斷返回零 - Swift iOS

class GreetingObjectHandler { 
    var greetings: [Greeting] = [] 
    var id: JSON? //this variable 
    init(filename: String) { 
     Alamofire.request(.GET, "http://localhost:2403/users/me") 
      .responseJSON { (req, res, data, error) in 
       if(error != nil) { 
        NSLog("Error: \(error)") 
       } 
       else { 
        var parse = JSON(data!) 
        self.id = parse["id"] //updating 
        println(self.id) //it's fine here 
       } 
     } 

     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) //returns nil 
     } 
    } 

    func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{ 

     return [Constant.GreetingOBJHandlerSectionKey: greetings.map { $0 as AnyObject }] 
    } 
} 
+2

request()是一個異步操作。你不能知道哪一行首先被調用,而不會像println(「1 \(self.if)」)那樣添加一些東西給它們。 –

+2

它返回nil,因爲它在主線程上運行並且請求操作還沒有成功完成,值的id屬性。 –

回答

-3

所有的建議都是有效的,但這是實際工作的。

import Foundation 
import Alamofire 

class GreetingObjectHandler { 
    var greetings: [Greeting] = [] 

    init(filename: String) { 

     let fileP = NSURL(string: "http://localhost:2403/users/me") 
     let jsonD = NSData(contentsOfURL:fileP!) 
     let jso = JSON(data: jsonD!, options: NSJSONReadingOptions.AllowFragments, error: nil) 
     var id = jso["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 != "\(id)"} 
     } 
    } 

    func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{ 

     return [Constant.GreetingOBJHandlerSectionKey: greetings.map { $0 as AnyObject }] 
    } 
} 
1

Alamofire請求異步運行,因此在初始化函數的末尾println聲明在responseJSON封尾的println語句之前運行。修復此問題的方法是運行需要responseJSON閉包中的json變量的代碼。

2

雖然人們的意見很好地解釋了您的問題,但我認爲使用閉包處理您的請求然後在完成處理程序中處理其餘部分可以幫助您組織代碼,類似於以下列方式:

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) 
    } 
} 

然後您可以通過以下方式在init撥打:

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) 
     } 
    } 
} 

與上面的代碼,你利用閉包來確定的,您的要求爲c ompleted。

我希望這對你有所幫助。

+1

它沒有經過。 –

+2

@SatnamSync你的意思是「它沒有經過」 –