[objective-c] Use NSInteger as array index

Have a happy year!

I am having trouble with the following. First logic:

Get json from server. Json value is always a number. Read plist into array. NSLog the array with index of the json value from server.

My plist:

Key     Type     Value Item 0  String   New York Item 1  String   Atlanta 

My code:

Get Json

NSString *url_req=@"http://domain.com/index.php";              NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url_req]];              NSData *response = [NSURLConnection sendSynchronousRequest:request                                                      returningResponse:nil error:nil];              NSError *jsonParsingError = nil;             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response                                                                       options:0 error:&jsonParsingError];              NSArray *cityArray = [json objectForKey:@"city"];             for (NSDictionary *alert in pestoArray ){                 myLoc = [[alert objectForKey:@"location"] integerValue];             } 

Json is working just fine!

Read plist and nslog the corresponding value:

NSString *path = [[NSBundle mainBundle] pathForResource:@"townsList" ofType:@"plist"];  NSArray *arrayNo= [[NSArray alloc] initWithContentsOfFile:path];              NSLog(@"City %@",arrayNo[myLoc]); 

So far arrayNo[myLoc] will return null but arrayNo[1] will return Atlanta.

How can i use the integer value from JSON as an array index?

Thank you!

EDIT: Error output when using arrayNo[myLoc] is

Indexing expression is invalid because subscript type 'NSInteger *' (aka 'int *') is not an integral or Objective-C pointer type 

This question is related to objective-c arrays json

The answer is

According to the error message, you declared myLoc as a pointer to an NSInteger (NSInteger *myLoc) rather than an actual NSInteger (NSInteger myLoc). It needs to be the latter.

Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?