2012-06-30 26 views
1

我正在做一個練習,我有2個班級的歌曲和播放列表,我可以把歌曲放在播放列表中,一切工作正常,但我需要讓一個主人播放列表中包含所有正常播放列表中的所有歌曲,並且存在問題。試圖在目標C中放置一個全局NSMutable陣列

下面是代碼

// 
// 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 

所以在播放列表中高手,它的缺失魯奇歌曲,希望你能幫助我,非常感謝你,我很感激

回答

0

我會讓你playlistMaster日的「靜態變量」 e播放列表實現文件,如在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方法釋放每一個這些。但是如果你不這樣做,他們可能會在你嘗試使用它們之前被釋放。

+0

ohhh把靜態變量是偉大的....它的工作原理,感謝您的時間和幫助! –