0

I'm having trouble adding objects to an NSMutableArray. I clearly place 2 objects in the typeList array, but the count only shows up as 1. What am I doing wrong?

content.h

@interface TBContentModel : NSObject

+(NSMutableArray*)typeList;
+(void)setTypeList:(NSMutableArray*)str; 

content.m

static NSMutableArray *typeList = nil;

@implementation TBContentModel

- (id) init {
    self = [super init];
    if (self) {
        typeList = [NSMutableArray array];
    }
    return self;
}

contentviewcontroller.m

@implementation TBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *jsonString = @"[{\"Content\":268,\"type\":\"text\"},{\"Content\":65,\"type\":\"number\"}]";
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSMutableDictionary *dictionary in array)
    {
        TBContentModel *test = [[TBContentModel alloc] init];
        test.type = dictionary[@"type"];
        [[TBContentModel typeList] addObject:test];
        NSLog(@"%@", test.type);
    }
}

- (IBAction)tapButton:(id)sender {
     NSLog(@"%d", [TBContentModel.typeList count]); // always shows 1
}
2
  • 5
    Please cut down the amount of code in your example and highlight where the issue is. Commented Jan 8, 2014 at 17:26
  • 1
    cut down the code. I'm a beginner so I wasn't sure what code was relevant to the problem. Commented Jan 8, 2014 at 18:44

1 Answer 1

1

You are re-creating your static typeList object everytime you allocate and initialize a new TBContentModel object.

Make the following changes:

static NSMutableArray *typeList = nil;
static dispatch_once_t once;

+ (NSMutableArray*)typeList {
    dispatch_once(&once, ^{
        typeList = [NSMutableArray array];
    });
    return typeList;
}

And remove the following line from your init method:

typeList = [NSMutableArray array];
Sign up to request clarification or add additional context in comments.

6 Comments

@Sancho You should remove your setTypeList: method as well.
ok. will do. I assume u suggest this because there's no reason for set now that I can add objects directly to it.
after making ur suggested edits, it appears to be adding the same object twice to the array rather than 2 objects. While it shows that 2 objects have been added, both objects are the same. Any ideas why?
@Sancho Have you examined the content of array to see what the processed JSON looks like?
The objects are going into the array correctly, but after the for loop is done, all objects are set to the last one that went in. From some research, it seems that it's pointing all objects in the array to *test. Perhaps I'm not initializing *test properly?
|

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.