0

私は今、音楽アプリに取り組んでいます (私が読んでいる本からの演習)。

これは私が今持っているコードです:

ソング.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

歌.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() を使用する list メソッドを宣言および定義しました。

main.m で print メソッドを使用しましたが、Xcode ウィンドウに出力がありません。

また、Playlist クラスに list メソッドを追加しました。このメソッドは、Song クラスの list メソッドを使用します。

それでもうまくいきません。

出力が得られない理由を説明できる人はいますか?

ありがとうございました!

4

2 に答える 2

1

すべての Song オブジェクトは nil です。nil オブジェクトのメソッドを呼び出しても効果はありません。使用する前に、それぞれを割り当てて初期化する必要があります。

song1 = [[Song alloc] init];
song2 = [[Song alloc] init];
... etc...
于 2013-06-09T00:08:27.293 に答える