Question

I create a Class with name and image. I use for loop to set NSMutableArray with set of object , The problem that after adding an object to the array the next round the array set the object to null as it remove the last object witch remove the object in the array here the code

#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;
}
Was it helpful?

Solution

Maybe it will not solve whole problem but you should change

@property (nonatomic,weak) NSString *name;

to

@property (nonatomic,strong) NSString *name;

because now you are loosing your names when leaving for loop.

OTHER TIPS

Try this:

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

And this:

- (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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top