2016-01-20 47 views
0

我正在進行網絡調用並從API中檢索一些信息並將其存儲在NSMutableArray中。在嘗試將該數組信息發送到另一個對象時,我碰壁了:
我沒有任何錯誤。我無法訪問另一個類中的數組信息。它在API類中打印效果很好,但當嘗試訪問另一個類時,數組會打印爲空。傳遞一個NSMutableArray到Swift中的另一個對象

這是我與一個NSMutableArray在最高層API類來保存信息:

class API: NSObject { 

    var informationArray = NSMutableArray(); 

    func getEarthquakeInformation() { 

     let session = NSURLSession.sharedSession() 
     let urlString = "http://ehp2-earthquake.wr.usgs.gov/fdsnws/event/1/query?format=geojson&limit=20" 
     let url = NSURL(string: urlString)! 
     let request = NSURLRequest(URL: url) 
     let task = session.dataTaskWithRequest(request){ data,response,downloadError in 

      if let error = downloadError { 
       print("could not complete the request\(error)") 
      } else { 

       let parsedResult = try! NSJSONSerialization.JSONObjectWithData(data! , options: NSJSONReadingOptions.AllowFragments) 
       let dataDict = parsedResult as! NSDictionary 

       // This holds all the information we need from the API. 
       if let result = dataDict["features"] { 

        for var i = 0; i < result.count; i++ { 
         self.informationArray.addObject(result[i]) 
         print(self.informationArray[i]) 
        } 

       } else { 
        print("error") 
       } 

       //print("info from the array \(self.informationArray)") 
      } 
     } 
     task.resume() 
    } 
} 

這是我試圖在發送到類:的MapViewController

​​
+1

您的來電(NSURLSession東西)是異步。在「print(self.informationArray [i])」之前打印「print(apiObject.informationArray)」。使用塊(關閉我認爲在Swift中) – Larme

+3

問題是什麼?你打的牆是什麼? – Cristik

+0

什麼是錯誤或打印輸出? – Ismail

回答

0
let apiObject = API() 
    // Your problem it here this method takes time to update informationArray 
    apiObject.getEarthquakeInformation() 
    // But this line print immediately while it's still empty. 
    print(apiObject.informationArray) 

你做的是

  • API類都刪除informationArray,並把它放在MapViewController
  • 變化getEarthquakeInformation方法有compeletion處理

    class API: NSObject { 
    
    // remove it //var informationArray = NSMutableArray(); 
    
    func getEarthquakeInformation(compeletion: (informationArray: [AnyObject]!) ->()) { 
    
        let session = NSURLSession.sharedSession() 
        let urlString = "http://ehp2-earthquake.wr.usgs.gov/fdsnws/event/1/query?format=geojson&limit=20" 
        let url = NSURL(string: urlString)! 
        let request = NSURLRequest(URL: url) 
        let task = session.dataTaskWithRequest(request){ data,response,downloadError in 
    
         if let error = downloadError { 
          print("could not complete the request\(error)") 
         } else { 
    
          let parsedResult = try! NSJSONSerialization.JSONObjectWithData(data! , options: NSJSONReadingOptions.AllowFragments) 
          let dataDict = parsedResult as! NSDictionary 
    
          // This holds all the information we need from the API. 
          if let result = dataDict["features"] { 
           compeletion(result) 
          } else { 
           print("error") 
          } 
         }  
        } 
    
        task.resume() 
    } 
    

然後在MapViewController

var informationArray: [AnyObject]! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // Instance of the api class 
    let apiObject = API() 
    apiObject.getEarthquakeInformation() { [unowned self] (result) ->() in 
    self.inforamtionArray = result 
    print(self.informationArray) 
} 
1

這裏有幾件事:

第一個,請使用聯網庫(如AlamoFire

,當你宣佈你的可變數組,你應該這樣做以這種形式(沒有理由使用NSMutableArray的()):

var myArray = [ObjectType]() 

,不要使用C風格for循環,他們被標記爲從Swift中刪除。您應該改爲迭代:

for item in result { 
    //Do something with item. 
} 

此外,據「送」的陣列到您的MapViewController對象。 如果API對象存在於您的MapViewController中,那麼您可以讓您的API函數將閉包作爲參數。您可以將數組傳遞迴閉包自身的MapViewController。 或者你也可以使用通知。

希望這會有所幫助。

相關問題