开发者

NSCFString objectAtIndex unrecognized selector sent to instance 0x5d52d70

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:

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

my View Controller.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:

#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];
[[NSNotificationC开发者_JAVA技巧enter 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.


In viewDidLoad change objects = [context executeFetchRequest:request error:&error]; to 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.

Using self.objects makes it use the synthesized setter and will retain it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜