objective-c - 使用 NSInteger 作为数组索引

标签 objective-c arrays json

祝你新年快乐!

我遇到以下问题。第一逻辑:

从服务器获取 json。 Json 值始终是一个数字。将 plist 读入数组。 NSLog 包含来自服务器的 json 值索引的数组。

我的plist:

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

我的代码:

获取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 工作正常!

读取plist和nslog对应的值:

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

到目前为止,arrayNo[myLoc] 将返回 null,但 arrayNo[1] 将返回 Atlanta。

如何使用 JSON 中的整数值作为数组索引?

谢谢!

编辑:使用 arrayNo[myLoc] 时的错误输出是

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

最佳答案

根据错误消息,您将 myLoc 声明为指向 NSInteger (NSInteger *myLoc) 的指针,而不是实际的 NSInteger (NSInteger myLoc)。应该是后者。

关于objective-c - 使用 NSInteger 作为数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863859/

相关文章:

ios - `[AVCaptureSession canAddOutput:output]` 间歇性地返回 NO。我能找出原因吗?

objective-c - 如何在 Cocoa 的窗口内处理 mousedown 事件

Java绘制具有随机颜色大小的图 block ?

c++ - STL 容器速度与数组

javascript - 选择列表过滤json数据

ios - UIDynamics 在 Swift 中不起作用

iphone - 按照本地格式显示日期

JavaScript 循环和数组

javascript - 将 MATLAB 连接到 Firebase 数据库

c# - 解析动态JSON数据时最好使用什么:动态类型内置的JToken或C#