2015-09-05 52 views
0

我正在將我的ViewControllerObjective-C轉換爲Swift從使用Swift解析數據保存對象而不是Objective-C

我在想,如果findObjectsInBackgroundWithBlock是仍然在使用(或者,如果將加載過慢)最聰明的事,如果我應該用同樣的方式。

我正在使用它獲取我的Parse數據並將其保存到我的App Group和/或NSUserDefaults

// Query Parse 
    PFQuery *query = [PFQuery queryWithClassName:@"data"]; 

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 

      NSMutableArray *localMatchup = [@[] mutableCopy]; 

      for (PFObject *object in objects) { 


       // Add objects to local Arrays 
       [localMatchup addObject:[object objectForKey:@"matchup"]]; 

       // App Group 
       NSString *container = @"group.com.ramsden.playoffs"; 
       NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container]; 

       // Matchup 
       [defaults setObject:localMatchup forKey:@"KeyMatchup"]; 
       NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"]; 
       self.matchupArray = localMatchup; 

更新: 我試過下面拉馬爾的答案,但我對findObjectsInBackgroundWithBlock行,說得到一個錯誤:'([AnyObject]!, NSError!) -> Void' is not compatible to 'PFArrayResultBlock?

enter image description here 這裏是在特定行仔細一看: enter image description here

回答

1

試試這個:

func GetBackFromParse(localMatchUp:NSMutableArray){ 
var query = PFQuery(className: "data") 
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in 

    if error == nil 
    { 

     if let objects = objects as? [PFObject]{ 

      for SingleObject in objects 
      { 

       var matchup = SingleObject["matchup"] 
       localMatchUp.addObject(matchup!) 

       // do whatever you want to do 
      } 

     } 
    } 

} 
+0

H ey @Lamar,謝謝你的迴應!我認爲你在做什麼對我來說是有意義的,到目前爲止唯一的事情是,我在'findObjectsInBackgroundWithBlock'行中得到錯誤:'([AnyObject]!,NSError!) - > Void'不是兼容'PFArrayResultBlock?'。任何想法? – SRMR

+0

替換我的代碼** query.findObjectsInBackgroundWithBlock {(對象:[AnyObject]?,錯誤:NSError?) - >無效** – Lamar

+0

是的,這是有效的。對於你放置'//做任何你想做的事情'的部分,是否與我的代碼中有'// App Group'和'Matchup'的部分相對應? – SRMR

相關問題