Eddwin Paz
Eddwin Paz

Reputation: 2868

How to load a JSON onto a NSArray

I need to pass to a Object a NSArray. In order it can show that array as Tags on the Interface, It works properly when using a manually added NSArray, But not i need to load a NSArray With a JSON Array Called Subjects I've done some code but it's not working out.

Also gives an error:

/Users/eddwinpaz/Documents/iOS-apps/mobile-app/mobile-app/UserDetailViewController.m:86:26: No visible @interface for 'NSArray' declares the selector 'insertObject:atIndex:'

This is the Code I'm Using

        NSArray *subject_tag;

        for (NSString* subject in [responseObject valueForKey:@"subjects"]) {

            [subject_tag insertObject:subject];
        }


        CGRect frame = Scroller.frame;

        frame.origin.y = 100;
        frame.origin.x = 5;
        frame.size.height = 150;

        UIPillsView *pillsView = [[UIPillsView alloc] initWithFrame:frame];

        [pillsView generatePillsFromStringsArray:subject_tag];

        [Scroller addSubview:pillsView];

Upvotes: 1

Views: 91

Answers (1)

Dima
Dima

Reputation: 23634

You have 3 problems here.

  1. You never initialize your array, you only declare it. (This one is not actually causing the error and would just cause the code to fail silently once you fixed the next 2)

  2. NSArrays are immutable. Elements cannot be added or removed after initialization. You need to use an NSMutableArray for this.

  3. The method you are using does not exist in NSMutableArray anyway.

Here is what you should be doing:

    NSMutableArray *subject_tag = [NSMutableArray new];

    for (NSString* subject in [responseObject valueForKey:@"subjects"]) {

        [subject_tag addObject:subject];
    }

Upvotes: 2

Related Questions