- [NSCFString objectAtIndex:]:无法识别的选择器

dav*_*av3 2 iphone cocoa-touch objective-c

我有一个我在谷歌找不到的小问题: UITableView工作正常,直到我开始滚动.

Error Message: `-[NSCFString objectAtIndex:]: unrecognized selector  sent to instance 0x5f24870 `
Run Code Online (Sandbox Code Playgroud)

方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *MyIdentifier = @"MyIdentifier";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];     
    if (cell == nil)
    { 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }   
    // Set up the cell 
    int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];     
    //populate cell
    cell.textLabel.text = [[myArray objectAtIndex: storyIndex] objectForKey: @"subject"]; 
    return cell;    
}
Run Code Online (Sandbox Code Playgroud)

只是猜到这是"问题" - 方法......如果您需要更多信息:请告诉我:)感谢您的帮助!

编辑:myArray只是正常NSArray *myArray 它是一个排序的数组 - >

   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lesson" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
myArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];
Run Code Online (Sandbox Code Playgroud)

resultArray(NSMutableArray)是XML-Parser的结果..希望你能看到我的问题..

Ste*_*ton 11

这意味着您objectAtIndex:要向类型NSString(或可能CFString)的对象发送消息.当然,你期望myArray成为一个数组,而不是一个字符串.

什么可能的情况是,你的阵列被释放(几乎可以肯定,自动释放)太早,通过上面的方法被调用的存储时间正指向由myArray现在拥有的字符串.

为了回应你的编辑......

这一行:

myArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];
Run Code Online (Sandbox Code Playgroud)

...是不正确的.该值是自动释放的.你需要保留它:

myArray = [[resultArray sortedArrayUsingDescriptors:sortDescriptors] retain];
Run Code Online (Sandbox Code Playgroud)

记得在关闭视图之前释放它!