Reputation: 2091
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!
Upvotes: 0
Views: 117
Reputation: 1191
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.
Upvotes: 0
Reputation: 6710
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...
Upvotes: 1