我现在正在制作一个音乐应用程序(从我正在阅读的书中练习。)
这是我现在的代码:
Song.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
Playlist.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
Playlist.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类中,我声明并定义了一个list方法,它使用NSLog()。
在main.m中我使用了print方法,但是我的Xcode窗口没有输出。
我还在Playlist类中添加了一个list方法,该方法使用Song类中的list方法。
即使这样也行不通。
这里有人可以向我解释为什么我没有得到任何输出吗?
谢谢!
答案 0 :(得分:1)
所有歌曲对象都是零。调用nil对象的方法将不起作用。你需要在使用前分配和初始化它们。
song1 = [[Song alloc] init];
song2 = [[Song alloc] init];
... etc...
答案 1 :(得分:0)
你必须做
-(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
方法。