0

I have class, that have several properties, it look like this:

@interface PlaceHolder : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *webPage;
@property (strong, nonatomic) NSNumber *latitude;

What i need is, to create an array, that hold objects of that class. Obvious, properties will not be nil and will be different each time. So, that array must have several hundreds of PlaceHolder object, and it should be possible to get information for any of that object and it properties. But, when i try to create that array, in NSLog i see that it contain only (NULL) objects. This is how i try too add object to array:

In header i wrote:

@property  (strong, nonatomic) PlaceHolder  *place;

Then:

self.place = [[PlaceHolder alloc]init];
    self.place.name = nameString;
    NSLog(@"%@ name???", self.place.name);
    [self.placeObjectsArray addObject:self.place];

self.place.name is not nil, and still, array is empty. Well, its not true, it not empty but, it only contains (null) objects. How to fill array with objects of my class?

Any advice would be appreciated, thanks!

UPDATED:

I init array like this -

-(id)initWithDelegate:(id)delegateObj{
    ...
    self.placeObjectsArray = [NSMutableArray array];
    ...
    return self;
}

UPDATED: Now, when i try to init-alloc array in same method (instead of setting @property and strong relation) i can see it in NSLog. I wonder why it won't happen when i use my array, that set as property..

5
  • 1
    Have you done self.placeObjectsArray=[[NSMutableArray alloc] init]; before using it ? Commented Jun 16, 2014 at 11:57
  • Show the array code, where it is created the array, not just declared it. Commented Jun 16, 2014 at 11:58
  • Please have a look at updating question. Commented Jun 16, 2014 at 12:04
  • Did you end up finding a solution here? Commented Apr 16, 2015 at 14:49
  • @Zil thats an old post. Im not remember my solution, but point was, that i got CORRECT array but for some reason it output as nil or Nil or Null is NSLog. But actually it hold values. Commented Apr 16, 2015 at 14:52

2 Answers 2

2

You need to alloc-init your Mutable Array ;

NSMutableArray *myArray = [[NSMutableArray alloc]initWithObjects:self.place,nil];

or simply

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

Then you could add objects with a for loop or whatever you need, using the following :

for ( YOURINSTRUCTION )
{
[myArray addObject:YOUROBJECT]
}
Sign up to request clarification or add additional context in comments.

Comments

1

I recommend to lazy instantiate the array, that way it will only get instantiated when really needed. Since you are setting the array as a property, you can override the getter method for it like this:

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

With this, you can call [self.placeObjectsArray addObject:self.place] anywhere in your code and the array will always be initialized when needed.

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.