Question

Newbie Questions,

I have 3 Class , 3 of them are subclass of NSObject

a. Collection Class

This class will have 2 property

  1. masterSongs (Contains all songs) declared as NSMUtableSet (copy, nonatomic)
  2. listofPlaylists (contains all playlists) declared as NSMutableArray (copy, nonatomic)

b. Playlist Class

This Class will have 2 property

  1. playListName declared as NSString
  2. songLists declared as NSMutableArray (strong, nonatomic), only store reference to the song not copy of song.

c. Songs Class

This Class has 4 property declared as NSString :

  1. title
  2. artist
  3. playingtime
  4. album

Questions :

  1. I want to create removeSong method for Collection class, so when I delete particular song from Collection (masterSongs), it will also delete song in all of playList stored in Collection.listofPlaylists. but I am stuck with syntaxes*.

*I am using lookup method to create a NSSet and then using that set to remove song object from NSMutableSet masterSongs using - minusSet: method

also, I found that it's dangerous to change modify NSMutableSet while enumerating on it.

here I tried so far :

- (NSSet *) lookUpTitle: (NSString *)aName {

NSPredicate *filter = [NSPredicate predicateWithFormat:@"title == %@",aName];

NSSet *result = [self.masterSongs filteredSetUsingPredicate:filter];

if ([result count] == 0) {
    NSLog(@"not found");

    return nil;
}


else{
    return result;
}

}

- (void) removeSong: (NSString *)zSong{


    for (Song *aSong in masterSongs) {



        if ([self lookUpTitle:zSong] != nil) {

            NSMutableSet *container = [NSMutableSet setWithSet:self.masterSongs];
        }

    }

}



 - (void) addSong :(Song *)aSong{
    if (![masterSongs containsObject:aSong]) {

        [masterSongs addObject:aSong];

    } }

-(void) addPlaylist: (Playlist *)aPlayList{

    if ([listOfPlaylists containsObject:aPlayList]==YES) {

    }
    else
        [listOfPlaylists addObject:aPlayList];


}

-(void) removePlaylist: (Playlist *)aPlayList{
    if ([listOfPlaylists containsObject:aPlayList]) {

        [listOfPlaylists removeObjectIdenticalTo:aPlayList];
    }

    else{
        ;
    }

}
Was it helpful?

Solution

-lookUpTitle: does most of the work for you. It returns a set of songs which need to be removed. You use -minusSet: to remove them from a NSMutableSet. To remove the songs from the playlists, you need to loop through your playlists and remove the songs in the set. You use -removeObjectsInArray: to remove them from a NSMutableArray.

- (void)removeSong:(NSString *)zSong
{
    NSSet *targets = [self lookUpTitle:zSong];

    if ([targets count] > 0) {
        [self.masterSongs minusSet:targets];

        for (Playlist *playlist in self.listOfPlaylists) {
            // -allObjects converts the set into an array.
            [playlist.songLists removeObjectsInArray:[targets allObjects]];
        }
    }
}

You asked a new question about properties.

@property (copy) NSMutableSet *masterSongs;

Is a bad idea. A -setMasterSongs: method is automatically generated which will look something like this:

- (void)setMasterSongs:(NSMutableSet *)masterSongs
{
    if (_masterSongs != masterSongs) {
        _masterSongs = [masterSongs copy];
    }
}

The problem is -[NSMutableSet copy] returns an NSSet; similarly, -[NSMutableArray copy] returns NSArray. This is why you ran into a problem. NSSet and NSArray are immutable, so they don't contain any methods which will change their contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top