0
votes

I have 2 core data classes - playlists and tracks. Playlists can hold many tracks.

Here is my code for creating a track and adding it to a playlist

    AppDelegate *del = [[AppDelegate alloc] init];
    NSManagedObjectContext *context = del.managedObjectContext;

    self.play = cell.obj;
    NSLog(@"play %@", self.play);


 [Track trackForStorage:self.track inManagedObjectContext:context];

    NSError *e2;
    [context save:&e2];



    if (e2 == nil) {

        NSString *entity2 = [NSString stringWithFormat:@"Track"];

        NSFetchRequest *fet2 = [NSFetchRequest fetchRequestWithEntityName:entity2];
        NSError *e3;

        NSArray *array2 = [context executeFetchRequest:fet2 error:&e3];



        if ([array2[0] isKindOfClass:[Track class]]) {
            Track * t = array2[0];
            NSLog(@"track %@", t);
            [self.play addTracksObject:t];


        }



    }

When I try adding a track to a playlist, i get the following error:

-[Playlist addTracksObject:]: unrecognized selector sent to instance 0x7fd76d911610 2016-04-25 16:27:10.875 My Music Safe[14899:1021546] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Playlist addTracksObject:]: unrecognized selector sent to instance 0x7fd76d911610'

edit: here is the "Playlist+CoreDataProperties.h"

@interface Playlist (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSSet<Track *> *tracks;

@end

@interface Playlist (CoreDataGeneratedAccessors)

- (void)addTracksObject:(Track *)value;
- (void)removeTracksObject:(Track *)value;
- (void)addTracks:(NSSet<Track *> *)values;
- (void)removeTracks:(NSSet<Track *> *)values;

@end
1
Can you share the interface of Playlist? It seems like you have a Playlist instance, but it really does not have an addTracksObject: method.user212514
I just added it. It was created in the "Playlist+CoreDataProperties.h" when I created the Playlist core data class.madgrand

1 Answers

0
votes

What is self.play? That is the object that is not responding to the message -addTracksObject:

Update 1

Is the implementation file included?

BTW, calling AppDelegate *del = [[AppDelegate alloc] init]; is very unusual, if you are doing that to get access to the NSManagedObjectContext you are going to run into issues.