我正在尝试将 Song* 对象添加到可变数组中,但我很困惑,因为尽管添加了对象,但数组的计数并未增加。
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
播放列表.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
播放列表.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;
}
我得到的播放列表中的条目为 0,歌曲列表为空。我没有收到任何编译错误,我不知道为什么对象没有添加到数组中。