Objective c 使用NSLog()时没有输出
我现在正在开发一个音乐应用程序(我正在阅读的书中的练习) 这是我现在拥有的代码: 宋h:Objective c 使用NSLog()时没有输出,objective-c,Objective C,我现在正在开发一个音乐应用程序(我正在阅读的书中的练习) 这是我现在拥有的代码: 宋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) setTitl
#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
播放列表.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
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
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.h”
#导入“Playlist.h”
#导入“MusicCollection.h”
int main(int argc,const char*argv[]
{
@自动释放池{
歌曲*song1、*song2、*song3、*song4、*song5;
播放列表*播放列表1、*播放列表2;
音乐收藏*收藏;
[歌曲1集标题:@“少年梦”和艺术家:@“凯蒂·佩里”和阿尔本:@“完整的糖果”和时间:@“3:48”];
[歌曲2集名:@“永远”和艺术家:@“泰勒·斯威夫特”和阿尔邦:零和时间:零];
[歌曲3集标题:@“焰火”和艺人:歌曲1.艺人和歌手:歌曲1.专辑和时间:无];
[歌曲4集标题:@“停留”和艺术家:歌曲2.艺术家安达尔邦:@“红色”和时间:零];
[歌曲5集名:@“22”和艺人:歌曲2.艺人和歌手:歌曲4.专辑和时间:无];
[播放列表1添加歌曲播放列表:歌曲1];
[播放列表1添加歌曲播放列表:歌曲3];
[播放列表2添加歌曲播放列表:歌曲1];
[播放列表2添加歌曲播放列表:歌曲2];
[播放列表2添加歌曲播放列表:歌曲5];
[收藏添加播放列表收藏:播放列表1];
[收藏添加播放列表收藏:播放列表2];
[歌曲1名单];
[播放列表2];
}
返回0;
}
在Song类中,我声明并定义了一个list方法,它使用NSLog()
在main.m中,我使用了print方法,但在Xcode窗口中没有输出
我还在Playlist类中添加了一个list方法,它使用Song类中的list方法
即使这样也不行
有没有人能向我解释为什么我得不到任何输出
谢谢大家! 您的所有歌曲对象均为零。调用nil对象的方法将无效。在使用之前,您需要分配和初始化它们中的每一个
song1 = [[Song alloc] init];
song2 = [[Song alloc] init];
... etc...
你必须做
-(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
方法。哦,我真是太蠢了。;-)但是现在我已经分配并初始化了所有内容,它仍然没有输出…不管怎样,我替换了所有设置为nil的歌曲信息为@“未知”,现在它给出了输出。奇怪的是,因为NSString可能是nil…@bbamhart现在我直接在歌曲对象上使用'list'方法时会得到输出,但是当我从Playlist使用'list'方法时,比如'[playlist2];'我没有输出。Playlist中的list方法使用Song中的list方法,因此它应该可以。。。我如何在注释中添加代码?你的应用程序在运行时会崩溃吗?song2 album为零,这将导致[NSString stringWithString:album]
崩溃。在设置详细信息时,只需在歌曲中使用album=theAlbum
,而不是stringWithString。我对此不确定。我在书中的一个示例中看到了它,并认为最好使用“stringWithString:”。
#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;
}
song1 = [[Song alloc] init];
song2 = [[Song alloc] init];
... etc...
-(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);
}