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;
}
Rajesh
  • 10,318
  • 16
  • 44
  • 64
MiM
  • 3
  • 1

2 Answers2

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.

Avt
  • 16,927
  • 4
  • 52
  • 72
  • Also, why use `nonatomic` anyway? – Romain Mar 28 '14 at 12:23
  • I am new to the language the I still didn't understand the full concept of the relationships , thanks that solve the problem . – MiM Mar 28 '14 at 12:32
  • @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](http://stackoverflow.com/questions/9859719/objective-c-declared-property-attributes-nonatomic-copy-strong-weak) – Pawan Rai Mar 28 '14 at 12:38
  • 1
    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. – gnasher729 Mar 28 '14 at 12:56
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;
}
nomada
  • 41
  • 4