Newbie Questions,
I have 3 Class , 3 of them are subclass of NSObject
a. Collection Class
This class will have 2 property
b. Playlist Class
This Class will have 2 property
c. Songs Class
This Class has 4 property declared as NSString :
Questions :
*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{
;
}
}
-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.