I am trying add a Song* object to a Mutable array and I am stumped as the count of the array is not increasing in spite of adding the object.
Song.h
#import <Foundation/Foundation.h>
@interface Song : NSObject
@property(copy, nonatomic) NSString *title, *album, *artist;
@property(copy, nonatomic) NSNumber *playTime;
@end
Song.m
#import "Song.h"
@implementation Song
@end
Playlist.h
#import <Foundation/Foundation.h>
@class Song;
@interface Playlist : NSObject
@property(copy, nonatomic) NSMutableArray *playListArray;
-(void) addSong: (Song *) tempSongToBeAdded;
-(void) removeSong: (Song *) tempSongToBeremoved;
-(void) listOfSongs;
-(NSUInteger) entries;
@end
Playlist.m
#import "Playlist.h"
#import "Song.h"
@implementation Playlist
-(void) addSong: (Song *) tempSongToBeAdded{
NSLog(@"%s song is being added.", [tempSongToBeAdded.title UTF8String]);
[self.playListArray addObject:tempSongToBeAdded];
}
-(void) removeSong: (Song *) tempSongToBeremoved{
[self.playListArray removeObject:tempSongToBeremoved];
}
-(NSUInteger) entries{
return [self.playListArray count];
}
-(void) listOfSongs{
NSLog(@"Hi");
for (Song *tempSong in self.playListArray) {
NSLog(@"title: %s", [tempSong.title UTF8String]);
}
}
@end
Main.m
#import <Foundation/Foundation.h>
#import "Song.h"
#import "Playlist.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Song *song1 = [[Song alloc] init];
song1.title = @"Manasa";
song1.album = @"Ye Maya Chesava";
song1.artist = @"A. R. Rahman";
song1.playTime = [NSNumber numberWithDouble:4.56];
Playlist *playlist1 = [[Playlist alloc] init];
[playlist1 addSong:song1];
NSLog(@"The total number of songs are %lu",[playlist1 entries]);
[playlist1 listOfSongs];
}
return 0;
}
I am getting the entries in the playlist as 0 and and the list of songs as empty. I am not getting any compile errors and I have no idea why the objects are not getting added to the array.
[nil count]always returns zero).[[NSMutableArray] alloc] init]is giving you a runtime error??