2

我有两个课程,歌曲课程和播放列表课程。

这是 Song.h:

#import <Foundation/Foundation.h>

@interface Song : NSObject

@property (nonatomic, copy) NSString *artist, *title, *album, *time;

-(void) setSong:(NSString *)theSongName andArtist:(NSString *)theArtist andAlbum:(NSString *)theAlbum andPlayingTime:(NSString *)theTime;

@end

宋.m

@implementation Song

@synthesize title, album, artist, time;

-(void) setSong:(NSString *)theSongName andArtist:(NSString *)theArtist andAlbum:(NSString *)theAlbum andPlayingTime:(NSString *)theTime{

    self.title = theSongName;
    self.artist = theArtist;
    self.album = theAlbum;
    self.time = theTime;
}

@end

播放列表.h

@interface PlayList : NSObject

@property (nonatomic, copy) NSString *playListName;
@property (nonatomic, copy) NSMutableArray *songsCollection;

-(void) addSongToPlayList:(Song *) someSong;
-(void) removeSongFromPlayList:(Song *) theSong;
-(void) print;

@end

播放列表.m:

@implementation PlayList

@synthesize songsCollection, playListName;

-(void) addSongToPlayList:(Song *) someSong{

    [songsCollection addObject:someSong];
}


-(void) removeSongFromPlayList:(Song *)theSong{

    [songsCollection removeObjectIdenticalTo:theSong];
}

-(void) print{

        NSLog(@"================= Playlist Name: %@ =============", playListName);
    for (Song *nextSong in songsCollection){
        NSLog(@"Artist         Song         Album         Time");
        NSLog(@"------         ----         -----         ----");
        NSLog(@"%s             %s           %s            %s  ", [nextSong.artist UTF8String], [nextSong.title UTF8String], [nextSong.album UTF8String], [nextSong.time UTF8String]);
        NSLog(@"=================================================");
    }
}

@end

print方法给我带来了问题,它只打印这一行:

NSLog(@"================= Playlist Name: %@ =============", playListName);

主文件

#import <Foundation/Foundation.h>
#import "Song.h"
#import "PlayList.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Song *song1 = [[Song alloc] init];
        [song1 setSong:@"Flying away" andArtist:@"Madona" andAlbum:@"Love collection" andPlayingTime:@"3:52"];

        PlayList *playList1 = [[PlayList alloc] init];
        [playList1 setPlayListName:@"Cool Soongs to listen"];


        [playList1 addSongToPlayList:song1];
        [playList1 print];



    }
    return 0;
}
4

3 回答 3

1

songsCollection问题是由于其属性的定义,您没有正确填充数组(即它是空的):

 @property (nonatomic, copy) NSMutableArray *songsCollection;

您需要将其更改为 (ARC):

 @property (nonatomic, strong) NSMutableArray *songsCollection;

或(MRR):

 @property (nonatomic, retain) NSMutableArray *songsCollection;

还将所有引用从 更改songsCollection为类self.songsCollectionPlayList

于 2013-03-05T15:55:29.153 回答
1

你还没有alloc+ inited songsCollection

在 PlayList.m 中创建一个init方法并放在下面

- (id)init
{
    self = [super init];
    if (self) {
        songsCollection=[NSMutableArray new];
    }
    return self;
}
于 2013-03-05T16:10:16.430 回答
0

试试这个。

NSLog(@"================= Playlist Name: %@ =============", self.playListName);

songsCollection数组可能没有有效的内存。使用强。在你的方法中实例化它

-(id)init
{
    self=[super init];
    self.songsCollection =[[NSMutableArray alloc]initWithCapacity:3];
    return self;
}
于 2013-03-05T16:04:03.740 回答