0

I am now working on a music-App (exercise from the book I'm reading.)

This is the code I now have:

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

In the Song class I have declared and defined a list method, which uses NSLog().

In main.m I used the print method, but I have no output on my Xcode windows.

I also added a list method in the Playlist class, which uses the list method from the Song class.

Even that isn't working.

Is here anyone who could explain to me why I don't get any output?

Thank you!

2 Answers 2

1

All your Song objects are nil. Calling a method of a nil object will have no effect. You need to alloc and init each of them before use.

song1 = [[Song alloc] init];
song2 = [[Song alloc] init];
... etc...
Sign up to request clarification or add additional context in comments.

5 Comments

Oh, that's really dumb of me. ;-) But now I have allocated and initialized everything, it still gives no output...
Nevermind, I replaced all the song info that was set nil to @"Unknown", and now it gives output. Strange, since a NSString could be nil...
@bbamhart Now I get output when I use the 'list' method directly on a song object, but when I use the 'list' method from Playlist, like '[playlist2 list];' I get no output. The list method from Playlist uses the list method from Song, so it should work... PS: How do I put code in comments?
Does your app crash when running? song2 album is nil which will cause [NSString stringWithString:album] to crash.
Just use album = theAlbum in Song when setting the detail information and not stringWithString.
0

You gotta do

-(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);
}

There is absolutely no reason to use the stringWithString method in your case either.

1 Comment

I wasn't sure about that. I saw it in an example in the book, and thought it was better to use 'stringWithString:'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.