NSMutableArray在for循环中将对象设置为null

时间:2014-03-28 12:15:34

标签: ios objective-c arrays nsmutablearray

我创建了一个包含名称和图像的类。我用for循环来设置NSMutableArray和一组对象, 在向数组添加一个对象后,下一轮数组将对象设置为null的问题,因为它删除了最后一个对象,删除了数组中的对象这里的代码

#import "MiMAppDelegate.h"

@interface MiMAnimal : NSObject

@property (nonatomic,weak) NSString *name;
@property (nonatomic,strong) UIImage *image;

- (instancetype) initWithName:(NSString *)name
                image:(UIImage *)image;

@end

@implementation MiMAnimal

-(instancetype)initWithName:(NSString *)name image:(UIImage *)image{
self =[super init];
if (self) {
    _name = name;
    _image = image;
}
return self;
}

- (NSString *)description {
return _name;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

NSMutableArray *Animals = [NSMutableArray array];
for (int i=1; i<30; i++) {
    //after the first loop here the array contain 0 the object with null value !!
            NSString *name =[[NSString alloc] initWithFormat: @"0%d",i ];
    UIImage *image = [UIImage imageNamed:[[NSString alloc] initWithFormat: @"0%d.png",i ]];
    NSLog(@"name : %@ ",name);
    MiMAnimal *animal =[[MiMAnimal alloc]initWithName:name image:image];
    [Animals addObject:animal];
            //Here the animals array add the object 
}

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

2 个答案:

答案 0 :(得分:2)

也许它不会解决整个问题,但你应该改变

@property (nonatomic,weak) NSString *name;

@property (nonatomic,strong) NSString *name;

因为现在你在离开时已经失去了你的名字。

答案 1 :(得分:0)

试试这个:

    #import "MiMAppDelegate.h"

@interface MiMAnimal : NSObject

@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) UIImage *image;

- (id) initWithName:(NSString *)nam
                image:(UIImage *)imag;

@end

@implementation MiMAnimal
@syntethize name,image;
-(id)initWithName:(NSString *)nam image:(UIImage *)imag{
self =[super init];
if (self) {
    self.name = nam;
    self.image = imag;
}
return self;
}

- (NSString *)description {
return self.name;
}

而且:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

NSMutableArray *animals = [[NSMutableArray alloc]init];
for (int i=1; i<30; i++) {
    //after the first loop here the array contain 0 the object with null value !!
    NSString *name =[NSString stringWithFormat: @"0%d",i ];
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat: @"0%d.png",i ]];
    NSLog(@"name : %@ ",name);
    MiMAnimal *animal =[[MiMAnimal alloc]initWithName:name image:image];
    [animals addObject:animal];
            //Here the animals array add the object 
}

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}