0

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;
}
4
  • Never alloc array in for loop. it clears last added objects into it. Commented Mar 28, 2014 at 12:18
  • @yourwish The array is not allocated in the for loop, is it? Commented Mar 28, 2014 at 12:20
  • 2
    for once name is defined as weak in your MiAnimal class and will be gone after the loop. Your Animals array will be gone after didFinishLaunching is done. Commented Mar 28, 2014 at 12:20
  • Volker is spot on. Animals array will be gone when the method exits. Commented Mar 28, 2014 at 12:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, why use nonatomic anyway?
I am new to the language the I still didn't understand the full concept of the relationships , thanks that solve the problem .
@Romain nonatomic is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading. Reference Link
Actually... if it is nonatomic, and different threads get and set the property simultaneously, things will go wrong. On the other hand, most things are designed in such a way that multiple threads getting and setting a property simultaneously won't give any useful results anyway, so "nonatomic" is fine, so nonatomic is what you should use unless you really, really thought hard about how multiple threads will be handled.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.