Objective c 尝试在目标C中放置全局NSMutable数组
我正在做一个练习,我有两个班的歌曲和播放列表,在程序中我可以把歌曲放在播放列表中,一切都很好,但我需要制作一个主播放列表,其中包含所有正常播放列表中的所有歌曲,这就是问题所在 这是密码Objective c 尝试在目标C中放置全局NSMutable数组,objective-c,Objective C,我正在做一个练习,我有两个班的歌曲和播放列表,在程序中我可以把歌曲放在播放列表中,一切都很好,但我需要制作一个主播放列表,其中包含所有正常播放列表中的所有歌曲,这就是问题所在 这是密码 // // main.m // MyItunes // // Created by Rodrigo López on 6/29/12. // Copyright (c) 2012 ITQ. All rights reserved. // #import <Foundation/Founda
//
// 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
因此,在play list master中,它缺少Rucci歌曲,希望您能帮助我,非常感谢,我很感激它我将使您的PlayMaster成为播放列表实现文件的“静态变量”,如所述:。您只需在任何其他方法之外声明并初始化.m文件。(别忘了确保它被保留。) 我还应该注意,播放列表类中存在一些内存问题。特别是,初始值设定项代码应保留成员变量,如:
-(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
方法来释放其中的每一个。但是如果您不这样做,它们可能会在您尝试使用它们之前被释放。ohhh使用静态变量非常好。。。。很好用,谢谢你的时间和帮助!!
-(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;
}