我正在练习,我有两个类歌曲和播放列表,在程序中我可以将歌曲放在播放列表中,一切正常,但我需要制作一个主播放列表,其中包含所有歌曲正常的播放列表,有问题。
这是代码
//
// main.m
// MyItunes
//
// Created by Rodrigo López on 6/29/12.
// Copyright (c) 2012 ITQ. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Songs.h"
#import "PlayList.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Songs *mySong1 = [Songs new];
Songs *mySong2 = [Songs new];
Songs *mySong3 = [Songs new];
PlayList *myPlayList1=[[PlayList alloc]initWithName:@"First"];
PlayList *myPlayList2=[[PlayList alloc]initWithName:@"Second"];
[mySong1 setTitle:@"Back In Black" setArtist:@"AC/DC" setAlbum:@"Back In Black" setPlayt:@"4:16"];
[mySong2 setTitle:@"Medicate" setArtist:@"AFI" setAlbum:@"Crash Love" setPlayt:@"4:21"];
[mySong3 setTitle:@"Rucci" setArtist:@"Austin TV" setAlbum:@"La ultima noche" setPlayt:@"4:39"];
[myPlayList1 addSong:mySong1];
[myPlayList1 addSong:mySong2];
[myPlayList2 addSong:mySong3];
[myPlayList1 showPlayList];
[myPlayList2 showPlayList];
[myPlayList1 showPlayList];
[myPlayList1 showAllSongs];
}
return 0;
}
//
// PlayList.m
// MyItunes
//
// Created by Rodrigo López on 6/29/12.
// Copyright (c) 2012 ITQ. All rights reserved.
//
#import "PlayList.h"
@implementation PlayList
@synthesize playListarray,playListName;
-(id) initWithName: (NSString *) name
{
if(self)
{
playListName = [NSString stringWithString: name];
playListarray = [NSMutableArray array];
playlistMaster=[NSMutableArray array ];
}
return self;
}
-(void) addSong: (Songs *) theSong
{
[playListarray addObject:theSong];
[playlistMaster addObject:theSong];
}
-(void) showPlayList
{
NSLog(@"Play List: %@", self.playListName);
for(Songs *theSong in playListarray)
{
NSLog(@"%@", theSong.title);
}
}
-(void) showAllSongs
{
NSLog(@"Play List: Master");
for(Songs *theSong in playlistMaster)
{
NSLog(@"%@", theSong.title);
}
}
-(NSUInteger) entries
{
return [playListarray count];
}
-(void) remove: (Songs *) theSong
{
[playListarray removeObjectIdenticalTo:theSong];
}
@end
问题在于NSMutable数组,我想全局声明数组,但我不知道怎么做,这是这个程序的输出:
2012-06-29 19:24:06.061 MyItunes[17184:707] Play List: First
2012-06-29 19:24:06.080 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.083 MyItunes[17184:707] Medicate
2012-06-29 19:24:06.084 MyItunes[17184:707] Play List: Second
2012-06-29 19:24:06.085 MyItunes[17184:707] Rucci
2012-06-29 19:24:06.089 MyItunes[17184:707] Play List: First
2012-06-29 19:24:06.090 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.091 MyItunes[17184:707] Medicate
2012-06-29 19:24:06.091 MyItunes[17184:707] Play List: Master
2012-06-29 19:24:06.092 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.093 MyItunes[17184:707] Medicate
所以在播放列表大师,它失踪的Rucci歌曲,希望你能帮助我,非常感谢你,我很感激
答案 0 :(得分:0)
我会将您的播放列表主播设为播放列表实现文件的“静态变量”,如下所述:Objective C Static Class Level variables。您只需在任何其他方法之外声明并初始化.m文件。 (不要忘记确保它被保留。)
我还应该注意PlayList类中存在一些内存问题。特别是,初始化代码应保留成员变量,如:
-(id) initWithName: (NSString *) name
{
if (self = [super init]) {
playListName = [[NSString stringWithString: name] retain]; // NOTE: You could also do: [[NSString alloc] initWithString: name];
playListarray = [[NSMutableArray array] retain]; // NOTE: You could also do: [[NSMutableArray alloc] init];
playlistMaster = [[NSMutableArray array] retain]; // ditto
}
return self;
}
如果您这样做,则需要添加dealloc
方法来释放其中的每一个。但如果你不这样做,他们可能会在你尝试使用之前被释放。