0

I'm very new to objective-c so be easy :-) I have a container object, "Data", who has a number of NSMutableArrays.

Data.h

@interface Data : NSObject{

    NSMutableArray *one;
    NSMutableArray *two;    
}

@property (nonatomic, retain) NSMutableArray *one;

@end

and would like to pass it to a load method in which case it will update each corresponding array in the Data class.

Parser.h

+ (Parser *)load:(Data*) store;

Parser.m

+ (Parser *)load:(Data *) store {
...

[store.one addObject:name.stringValue];
}

But no matter what I do the string in "name.stringValue" doesn't get appended to the array. Is there something I'm missing when passing in the "Store" data object to the parse method? Let me know if I should provide more details but I feel this covers the issue.

6
  • 1
    Are you sure your array is properly initialized? Just having a property doesn't initialize it, it may just be nil and sending a message to nil doesn't throw an exception. Commented Jan 28, 2015 at 5:44
  • r u allocating the memory of array Commented Jan 28, 2015 at 5:47
  • + (NSMutableArray *)load:(Data *) store { NSMutableArray * store = [[NSMutableArray alloc]init]; for(NSString *str in store) { [store addObject:name.stringValue]; } return store; } try like this Commented Jan 28, 2015 at 5:53
  • Spynet - I get an error since that is redefining what "store" is. It's being passed in as a "Data" type whereas your setting it to NSMutableArray. Commented Jan 28, 2015 at 5:58
  • I tried initializing it in the parse method by doing: store.one = [[NSMutableArray alloc]init]; but no luck Commented Jan 28, 2015 at 6:02

2 Answers 2

1

Check in your implementation of Data that you are properly initializing the mutable arrays - here is a simple example below given your Data interface:

#import "Data.h"

@implementation Data
{

#pragma mark - Properties

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

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

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

6 Comments

Excuse the newbie comment but why is there a "one" and a "_one"? Is the underscored _one being magically generated somewhere?
Yeh, it's the default instance accessor... to access the variable directly, use _one, to access it through the getter within the class use, self.one, to access it outside of the class use, instanceObjectName.one.
Thanks. I'm just getting back into compiled code after quite some time so the gears are slowly turning :-) This approach, while it works, seems really verbose for no more than initializing an array. Is there a more concise way to perform this action short of one main 'init' function?
One more question while were on the subject of constructors/initialization. How would i go about creating an NSArray of class properies so i could loop through and allocate memory? Haven't gotten very far with using NSArray initWithObjects. Thanks!
To your earlier comment, you can also override your constructor and initialize your arrays... if you search for, "Objective-c override constructor" you will find more information.
|
0

Use one = [NSMutableArray array]; before you start using it. This will create an empty mutable array.

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.