我的打印方法不能使用UTF8String

时间:2013-03-05 15:51:32

标签: objective-c

我有两个类,Song类和播放列表。

这是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

Song.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

Playlist.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

Playlist.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);

的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 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;
}

3 个答案:

答案 0 :(得分:1)

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

 @property (nonatomic, copy) NSMutableArray *songsCollection;

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

 @property (nonatomic, strong) NSMutableArray *songsCollection;

或(MRR):

 @property (nonatomic, retain) NSMutableArray *songsCollection;

同时更改songsCollection课程中self.songsCollectionPlayList的所有引用。

答案 1 :(得分:1)

您尚未alloc + init编辑songsCollection

在PlayList.m中创建一个init方法并放在那里

- (id)init
{
    self = [super init];
    if (self) {
        songsCollection=[NSMutableArray new];
    }
    return self;
}

答案 2 :(得分:0)

试试这个。

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

songsCollection数组可能没有有效的内存。使用strong。在方法中实现它

-(id)init
{
    self=[super init];
    self.songsCollection =[[NSMutableArray alloc]initWithCapacity:3];
    return self;
}