gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:35:58 28 4
gpt4 key购买 nike

我是 iPhone 开发新手,一直很难弄清楚为什么我的 table 无法工作。我不确定,这可能与核心数据有关。 viewDidLoad 方法一开始工作正常,但是当我尝试滚动 TableView 时,当出现下一行时,我收到错误:

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

我的 View Controller .h:

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

@interface RostersViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {

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

我的 View Controller .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

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

最佳答案

在 viewDidLoad 中将 objects = [contextexecuteFetchRequest:request error:&error]; 更改为 self.objects = [contextexecuteFetchRequest:request error:&error];

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

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

关于objective-c - NSCFString objectAtIndex 无法识别的选择器发送到实例 0x5d52d70,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3170410/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com