1

我正在做一个练习,我有 2 类歌曲和播放列表,在程序中我可以将歌曲放入播放列表中,一切正常,但我需要制作一个包含所有正常播放列表中的所有歌曲的主播放列表和有问题。

这是代码

    //
//  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的歌曲,希望你能帮助我,非常感谢你,我很感激

4

1 回答 1

0

I would make your playlistMaster a "static variable" of the PlayList implementation file as described at: Objective C Static Class Level variables. You just declare and initialize it the .m file outside of any other methods. (Don't forget to make sure it is retained.)

I should also note that there are some memory issues in your PlayList class. In particular, your initializer code should retain the member variables, like:

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

If you do this, you will need to add a dealloc method that releases each of these. But if you don't they may be released before you try to use them.

于 2012-06-30T00:47:56.277 回答