开发者

Program received signal: EXC_BAD_ACCESS

开发者 https://www.devze.com 2023-03-18 15:10 出处:网络
I have the following method in my UITableViewController subclass: -(void)populateDataStorage{ NSString *path = [[NSBundle mainBundle] pathForResource:@\"FakeData\" ofType:@\"plist\"];

I have the following method in my UITableViewController subclass:

-(void)populateDataStorage{

NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"];

if(path){
    NSArray *plistData = [[NSArray alloc] initWithContentsOfFile:path];
    NSEnumerator *enumerator = [plistData objectEnumerator];

    NSArray *personResults;

    Photo *photo;
    Person *person;

    id currItem = [enumerator nextObject];

    while (currItem != nil) {
        photo = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext: [[FlickrFetcher sharedInstance] managedObjectContext]];

        photo.name = [currItem objectForKey:@"name"];
        photo.path = [currItem objectForKey:@"path"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", [currItem objectForKey:@"user"]];

        personResults 开发者_JS百科= [[FlickrFetcher sharedInstance] fetchManagedObjectsForEntity:@"Person" withPredicate:predicate];

        if ([personResults count] > 0) {
            person = [personResults objectAtIndex:0];
        } 
        else {
            person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[[FlickrFetcher sharedInstance] managedObjectContext]];
            person.name = [currItem objectForKey:@"user"];
        }

        photo.person = person;
        [person addPhotosObject:photo];

        NSLog(@"Photo %@ added for user %@", photo.name, person.name);

        currItem = [enumerator nextObject];
    }

    [plistData release];
}

}

And I call it in my apps didFinishLaunchingWithOptions method in my AppDelegate, the method is also in the same AppDelegate, when it's called I get the error it says there when debugging, if I don't debug the line it will run the method and load with no problem. If I don't debug at all it will not call the method.

EDIT: Changed the code according to the answer the problem still remains the same, if I just run nothing happens but when debugging I get the error. When I debug the whole method no error is shown.


Your call to NSLog is trying to access the name property of your person variable. However, when you declared your person variable, you didn't initialize it, so it points to garbage. You only give it a valid value in your else clause, so sometimes your NSLog is accessing an uninitialized object.

0

精彩评论

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

关注公众号