2013-05-30 34 views
0

我運行也沒有問題的查詢,這裏是我的代碼:傳遞查詢結果從解析到的NSArray的iOS

PFQuery *postQuery = [PFQuery queryWithClassName:@"class"]; 
[postQuery whereKey:@"hasRelationship" equalTo:[PFUser currentUser]]; 
// Run the query 
[postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     //Save results and update the table 
     group = [objects valueForKey:@"groupArray"]; 
    } 
} 

結果是好的,組控制檯打印如下:

(
("Register one", 
"Register two", 
"Register three", 
"Register four") 
) 

但我需要有長度爲4不是1每次我通過將它們通入一個NSString「可變拷貝」像這樣崩潰剝離的結果的數組:

(NSString *s = (NSString *)[group objectAtIndex:0]; 
NSString * text = (NSString *)[s mutableCopy];) 

以下是錯誤:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent *** 

我怎樣才能獲得對象內部的元素融入長度爲4的NSArray?謝謝!

+0

鑄造並不神奇,它只是欺騙編譯器,以爲你的對象是一個字符串,而它確實是一個陣列。 – 2013-05-30 14:13:26

+0

檢查如何創建打印到控制檯中的對象。你有一個包含另一個NSArray的NSArray。你的第一個數組有count = 1,因爲它包含另一個NSArray。你的第二個數組是需要的,它的count = 4;) – danypata

+0

我終於明白了,但是感謝你的評論。這裏是解決方案://將結果保存到數組中: group = [[objects valueForKey:@「groupArray」] objectAtIndex:0]; – user2436826

回答

0

簡單的回答這個。您的查詢返回了一個結果數組,其中包含四個對象。鑄造不會改變基本對象的類型,它只是欺騙編譯器認爲它處理特定對象的類。它實際上並沒有改變那個班級。

在這個特殊的例子中,它很簡單。在當前響應的索引0處已經有一個數組:

NSArray * allGroups = group [0];

這應該有一個單一的數組中的所有四個字符串,可以枚舉做你想做的任何事情。

+0

不能這樣做:***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [PFObject countByEnumeratingWithState:objects:count:]:無法識別的選擇器,我找到了答案並在上面發佈它,謝謝! – user2436826

+0

您在評論中發佈的答案實際上是相同的。使用: - [anArray objectAtIndex:0]訪問數組在語法上與使用anArray [0]訪問它相同; –

0

看看他們的示例代碼,看看如何使用數組objects。因此,改變你的if statement使用本:

if (!error) { 
    // The find succeeded. 
    NSLog(@"Successfully retrieved %d scores.", objects.count); 
    // Do something with the found objects 
    for (PFObject *object in objects) { 
     NSLog(@"%@", object.objectId); 
     // add to your array here 
    } 
    } else { 
    // Log details of the failure 
    NSLog(@"Error: %@ %@", error, [error userInfo]); 
    } 
} 

或簡單的東西,因爲這:

if (!error) { 
    // copy the array 
    [group addObjectsFromArray:objects]; 
}