user2539621
user2539621

Reputation: 361

Problems using Core Data generated accessors

So I've got two entities, one called "List" and one called "Task". Each of them has multiple attributes and one relationship. The relationship for List is called "hasTasks" and it is a one to many relationship.

This is the Task.h file that is generated for me.

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Task;

@interface List : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * number;
@property (nonatomic, retain) NSNumber * tasks;
@property (nonatomic, retain) NSNumber * totalTime;
@property (nonatomic, retain) NSSet *hasTasks;
@end

@interface List (CoreDataGeneratedAccessors)

- (void)addHasTasksObject:(Task *)value;
- (void)removeHasTasksObject:(Task *)value;
- (void)addHasTasks:(NSSet *)values;
- (void)removeHasTasks:(NSSet *)values;

@end

Now whenever I call something along the lines of

[self.list addHasTasksObject:task];

my app crashes when it gets to this point. Anybody have an idea why this is? If you need to see any more of my code, don't hesitate to ask. Thanks in advance!

Upvotes: 3

Views: 1836

Answers (2)

brad.roush
brad.roush

Reputation: 607

I had this same problem and I fixed it in the .xcdatamodeld file. I had accidentally selected the ordered arrangement checkbox for a relationship without re-generating the NSManagedObject classes. Unchecking this box and re-building solved this problem for me. If you are still having a problem, try re-generating your NSManagedObject classes.

Upvotes: 1

raja
raja

Reputation: 95

will u please post your core data add objects method.Let me see that method.Try this..

- (void)addButtonClicked:(id)sender

{

    if (![nameField.text isEqualToString:@""])

    {

    AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;

    ///Entity declaration....

        CountryNames *country = (CountryNames *) [NSEntityDescription insertNewObjectForEntityForName:@"CountryNames"
                                        inManagedObjectContext:appDelegate.managedObjectContext];
        ///take a array for relation ship atribute...
        for (int i = 0; i < citiesArray.count; i++)
        {   ///atribute declaration...
            CityNames *citiesNames = (CityNames *) [NSEntityDescription insertNewObjectForEntityForName:@"CityNames"
                                        inManagedObjectContext:appDelegate.managedObjectContext];

            [country addRelationtocitiesObject:citiesNames];

            citiesNames.cityName = [citiesArray objectAtIndex:i];
            NSLog(@"%@",citiesNames.cityName);

            NSError *error;

            if (![appDelegate.managedObjectContext save:&error]) {}

        }

        country.name = nameField.text;

        NSError *error;

        if (![appDelegate.managedObjectContext save:&error]) {}
        //  ListOfCountryDetails *viewList=[[ListOfCountryDetails alloc]init];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

Upvotes: 1

Related Questions