【问题标题】:No output while using NSLog()使用 NSLog() 时没有输出
【发布时间】:2013-06-09 08:25:46
【问题描述】:

我现在正在开发一个音乐应用程序(我正在阅读的书中的练习。)

这是我现在拥有的代码:

歌曲.h:

#import <Foundation/Foundation.h>

@interface Song : NSObject

@property (copy, nonatomic) NSString *title, *artist, *album, *time; //creat the 4 instance variables for the song info

-(void) setTitle:(NSString *)theTitle andArtist:(NSString *)theArtist andAlbum:(NSString *)theAlbum andTime:(NSString *)theTime; //set all info with one method

-(void) list; //print the song info

@end

Song.m:

#import "Song.h"

@implementation Song

@synthesize title, artist, album, time;

-(void) setTitle:(NSString *)theTitle andArtist:(NSString *)theArtist andAlbum:(NSString *)theAlbum andTime:(NSString *)theTime {
    title = [NSString stringWithString:theTitle];
    artist = [NSString stringWithString:theArtist];
    album = [NSString stringWithString:theAlbum];
    time = [NSString stringWithString:theTime];
}

-(void) list {
    if (title != nil)
        NSLog(@"Title: %@", title);
    if (artist != nil)
        NSLog(@"Artist: %@", artist);
    if (album != nil)
        NSLog(@"Album: %@", album);
    if (time != nil)
        NSLog(@"Time: %@", time);
}

@end

播放列表.h:

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

@interface Playlist : NSObject

@property (copy, nonatomic) NSString *playlistName; //name of the playlist
@property (copy, nonatomic) NSMutableArray *playListOfSongs; //songs stored in mutable array

-(void) addSongToPlaylist:(Song *)song; //add song to playlist-array

-(id) initWithPlaylistName:(NSString *)name; //init playlist object and set name

-(void) list; //list all song information for the songs in the playlist-array. Here the list-method from the Song-class is used

@end

播放列表.m:

#import "Playlist.h"

@implementation Playlist

@synthesize playlistName, playListOfSongs;

-(void) addSongToPlaylist:(Song *)song {
    [playListOfSongs addObject:song];
}

-(id) initWithPlaylistName:(NSString *)name {
    self = [super init];

    if (self) {
        playlistName = [NSString stringWithString:name];
    }
    return self;
}

-(id) init {
    return [self initWithPlaylistName:@"Unknown Playlist"];
}

-(void) list {
    for (Song *nextSong in playListOfSongs) {
        [nextSong list];
    }
}

@end

MusicCollection.h:

#import <Foundation/Foundation.h>
#import "Playlist.h"

@interface MusicCollection : NSObject

@property (copy, nonatomic) NSMutableArray *collection; //playlists stored in an array

-(void) addPlaylistToCollection:(Playlist *)playlist; //add playlist to collection-array

@end

Musiccollection.m:

#import "MusicCollection.h"

@implementation MusicCollection

@synthesize collection;

-(void) addPlaylistToCollection:(Playlist *)playlist {
    [collection addObject:playlist];
}

@end

main.m:

#import <Foundation/Foundation.h>
#import "song.h"
#import "Playlist.h"
#import "MusicCollection.h"

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

    @autoreleasepool {

        Song *song1, *song2, *song3, *song4, *song5;
        Playlist *playlist1, *playlist2;
        MusicCollection *collection;

        [song1 setTitle:@"Teenage Dream" andArtist:@"Katy Perry" andAlbum:@"The Complete Confection" andTime:@"3:48"];
        [song2 setTitle:@"Forever And Always" andArtist:@"Taylor Swift" andAlbum:nil andTime:nil];
        [song3 setTitle:@"Firework" andArtist:song1.artist andAlbum:song1.album andTime:nil];
        [song4 setTitle:@"Stay Stay Stay" andArtist:song2.artist andAlbum:@"Red" andTime:nil];
        [song5 setTitle:@"22" andArtist:song2.artist andAlbum:song4.album andTime:nil];

        [playlist1 addSongToPlaylist:song1];
        [playlist1 addSongToPlaylist:song3];

        [playlist2 addSongToPlaylist:song1];
        [playlist2 addSongToPlaylist:song2];
        [playlist2 addSongToPlaylist:song5];

        [collection addPlaylistToCollection:playlist1];
        [collection addPlaylistToCollection:playlist2];

        [song1 list];

        [playlist2 list];

    }
    return 0;
}

在 Song 类中,我声明并定义了一个使用 NSLog() 的列表方法。

在 main.m 中我使用了 print 方法,但我的 Xcode 窗口上没有输出。

我还在 Playlist 类中添加了一个 list 方法,它使用了 Song 类中的 list 方法。

即使这样也行不通。

这里有人可以向我解释为什么我没有得到任何输出吗?

谢谢!

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    你所有的 Song 对象都是 nil。调用 nil 对象的方法将不起作用。您需要在使用前分配并初始化它们中的每一个。

    song1 = [[Song alloc] init];
    song2 = [[Song alloc] init];
    ... etc...
    

    【讨论】:

    • 哦,我真傻。 ;-) 但是现在我已经分配并初始化了所有东西,它仍然没有输出......
    • 没关系,我将所有设置为 nil 的歌曲信息替换为 @"Unknown",现在它给出了输出。奇怪,因为 NSString 可能是 nil...
    • @bbamhart 现在,当我直接在歌曲对象上使用“list”方法时,我得到输出,但是当我使用播放列表中的“list”方法时,例如“[playlist2 list];”我没有输出。 Playlist 中的 list 方法使用 Song 中的 list 方法,所以它应该可以工作... PS:如何将代码放入 cmets?
    • 您的应用在运行时会崩溃吗? song2 专辑为 nil,这将导致 [NSString stringWithString:album] 崩溃。
    • 设置详细信息时只在Song中使用album = theAlbum而不是stringWithString。
    【解决方案2】:

    你必须这样做

    -(void) setTitle:(NSString *)theTitle andArtist:(NSString *)theArtist andAlbum:(NSString *)theAlbum andTime:(NSString *)theTime {
        self.title = theTitle;
        self.artist = theArtist;
        self.album = theAlbum;
        self.time = theTime;
    }
    
    -(void) list {
        if (title != nil)
            NSLog(@"Title: %@", self.title);
        if (artist != nil)
            NSLog(@"Artist: %@", self.artist);
        if (album != nil)
            NSLog(@"Album: %@", self.album);
        if (time != nil)
            NSLog(@"Time: %@", self.time);
    }
    

    在您的情况下也绝对没有理由使用stringWithString 方法。

    【讨论】:

    • 我不确定。我在书中的一个例子中看到它,并认为使用'stringWithString:'会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多