xcode NSCFString objectAtIndex 无法识别的选择器发送到实例 0x5d52d70

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3170410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:30:33  来源:igfitidea点击:

NSCFString objectAtIndex unrecognized selector sent to instance 0x5d52d70

objective-ciphonexcode

提问by quiksil12

I'm new to iPhone development, and been having a hard time trying to figure out why my table isn't working. It could be something with Core Data, I'm not sure. The viewDidLoad method works fine at the start, but when I try to scroll the table view, when a next row appears, I get the error:

我是 iPhone 开发的新手,一直很难弄清楚为什么我的桌子不工作。这可能与 Core Data 有关,我不确定。viewDidLoad 方法在开始时工作正常,但是当我尝试滚动表视图时,出现下一行时,我收到错误消息:

NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d52d70'

NSInvalidArgumentException',原因:'-[NSCFString objectAtIndex:]:无法识别的选择器发送到实例 0x5d52d70'

my View Controller.h:

我的视图控制器.h:

#import <UIKit/UIKit.h>
#define kTableViewRowHeight 66

@interface RostersViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource> {

NSArray *objects;
}
@property(nonatomic,retain) NSArray *objects;
@end

my View Controller.m:

我的视图控制器.m:

#import "RostersViewController.h"
#import "CogoAppDelegate.h"
#import "TeamCell.h"

@implementation RostersViewController
@synthesize objects;

- (void)viewDidLoad {
CogoAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Teams"                                            inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];

NSError *error;

objects = [context executeFetchRequest:request error:&error];

if (objects == nil)
{
    NSLog(@"There was an error!");
    // Do whatever error handling is appropriate
}   
[request release];

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(applicationWillTerminate:)
                                        name:UIApplicationWillTerminateNotification 
                                        object:app];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[objects release];
[super dealloc];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [objects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
id currObj = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = [currObj valueForKey:@"name"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kTableViewRowHeight;
}
@end

Thanks for any help. It's very much appreciated.

谢谢你的帮助。非常感谢。

回答by Joshua Weinberg

In viewDidLoad change objects = [context executeFetchRequest:request error:&error];to self.objects = [context executeFetchRequest:request error:&error];

在 viewDidLoad 中更改objects = [context executeFetchRequest:request error:&error];self.objects = [context executeFetchRequest:request error:&error];

executeFetchRequest returns an autoreleased object, which you are then storing directly to the ivar, this turns into a garbage pointer at a later point. Which just so happens to end up pointing to a string.

executeFetchRequest 返回一个自动释放的对象,然后您将其直接存储到 ivar,稍后会变成垃圾指针。这恰好最终指向一个字符串。

Using self.objectsmakes it use the synthesized setter and will retain it.

Usingself.objects使它使用合成的 setter 并保留它。