0

我正在尝试将 NSMutableArray 添加到其他 NSMutableArray,这是一个设置为实例 ivar 的可变数组,但我总是让数组 arrayPlayoff 为空。我检测到的另一件事是,在附加方法中进行调试时,局部变量不会显示在调试器变量部分中,即使选择“本地”选项也是如此。

-(void)loadGamesPlayoffs{

    NSMutableArray *eli1 = [[NSMutableArray alloc] init];

    NSMutableArray *eli2 =[[NSMutableArray alloc] init];

    NSMutableArray *eli3 = [[NSMutableArray alloc] init];

    NSMutableArray *eli4 = [[NSMutableArray alloc] init];

    for (NSDictionary *eliminatoria in copaReyArray){

        int eli = [[eliminatoria valueForKey:@"eliminatoria"]integerValue];

        NSLog(@"eli %d", eli);

        if (eli==1){

            [eli1 addObject:eliminatoria];

        } else if (eli==2){

            [eli2 addObject:eliminatoria];

        }else if (eli==3){

            [eli3 addObject:eliminatoria];

        } else if (eli==4){

            [eli4 addObject:eliminatoria];

        }


    }


    [arrayPlayOff addObject:eli1];

    [arrayPlayOff addObject:eli2];

    [arrayPlayOff addObject:eli3];

    [arrayPlayOff addObject:eli4];


   }

 many thanks   
4

1 回答 1

1

如果arrayPlayOff是一个实例变量,你可以使用属性,你的 .h 应该是这样的:

#import <UIKit/UIKit.h>
@interface YourClassViewController : UIViewController{
   NSMutableArray *arrayPlayOff;
   //Some other variables
}

@property (nonatomic, retain) NSMutableArray *arrayPlayOff;

//Some other methods

@end

然后在您的 .m 文件中,您可以使用:

@implementation YourClassViewController

@synthesize arrayPlayOff;

- (NSMutableArray *)arrayPlayOff{
  if(!arrayPlayOff){
    arrayPlayOff = [[NSMutableArray alloc] init];
  }
  return arrayPlayOff;
}

//Your other methods

//Overwrite the dealloc function so you don't have any memory leaks
- (void)dealloc{
  [arrayPlayOff release];
  [super dealloc];
}

@end

现在你可以访问你的变量

self.arrayPlayOff

这样你就可以 100% 确定你的变量总是被初始化的。

你的方法可能看起来像这样(你也应该释放你的其他 NSMutableArrays,因为在将它们添加到 arrayPlayOff 数组后你没有使用它们):

-(void)loadGamesPlayoffs{

    NSMutableArray *eli1 = [[NSMutableArray alloc] init];

    NSMutableArray *eli2 =[[NSMutableArray alloc] init];

    NSMutableArray *eli3 = [[NSMutableArray alloc] init];

    NSMutableArray *eli4 = [[NSMutableArray alloc] init];

    for (NSDictionary *eliminatoria in copaReyArray){

        int eli = [[eliminatoria valueForKey:@"eliminatoria"]integerValue];

        NSLog(@"eli %d", eli);

        if (eli==1){

            [eli1 addObject:eliminatoria];

        } else if (eli==2){

            [eli2 addObject:eliminatoria];

        }else if (eli==3){

            [eli3 addObject:eliminatoria];

        } else if (eli==4){

            [eli4 addObject:eliminatoria];

        }


    }


    [self.arrayPlayOff addObject:eli1];

    [self.arrayPlayOff addObject:eli2];

    [self.arrayPlayOff addObject:eli3];

    [self.arrayPlayOff addObject:eli4];

    //Releasing the NSMutableArrays
    [eli1 release]; 
    [eli2 release]; 
    [eli3 release]; 
    [eli4 release];

}
于 2012-05-22T18:58:23.747 回答