我想知道如何通過NSMutableArray迭代工作,但是當我調用objectAtIndex時,它會失敗,併發送「*** - [NSCFSet objectAtIndex:]:無法識別的選擇器發送到實例0x。 ..「NSCFSet objectAtIndex無法識別的選擇器,迭代工作
下面是一些示例代碼,該程序太大,無法共享,所以我希望這已經足夠了。該代碼在XCode的最新iPhone Simulator中執行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDLogString(@"Creating cell @ row no. %d", indexPath.row);
CPPlayerAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
RDLogString(@"Conversations: %p. Item count: %d", appDelegate.distribution.conversations, appDelegate.distribution.conversations.count);
//This works
for(CPConversation * x in appDelegate.distribution.conversations){
RDLogString(@"Pointer: %p with name %@", x, x.name);
}
//This fails with aforementioned error
CPConversation * conversationAtCurrentIndex = [appDelegate.distribution.conversations objectAtIndex: indexPath.row];
對不起,格式不好,仍然搞清楚。 :)
由於提前, 尼克
感謝這兩個!我已經將它定義爲一個NSMutableArray,但我進一步用[[NSMutableSet alloc] init]初始化它; xD 有時我會錯過C(++)的靜態類型。它一定是一個警告,但我有這麼多......我需要更多地關注這些。總之,非常感謝。 – Nick 2009-11-29 00:18:16
是的,不需要編輯。這甚至不是一個警告。我想,小心一點。任何建議來發現這樣的錯誤? – Nick 2009-11-29 00:19:50
問題是,NSMutableArray的指定初始化程序 - 連同NSSet和NSDictionary - 被聲明爲返回(id),因此任何類上的任何方法都被認爲是有效的。他們被這樣聲明的原因是因爲Objective-C不支持協變聲明。 – bbum 2009-11-29 00:36:36