使用NSInteger作为数组索引

 LIN-少爷 发布于 2023-02-07 11:54

祝你有快乐的一年!

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

从服务器获取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]将返回亚特兰大.

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

谢谢!

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

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

Chuck.. 8

根据错误消息,您声明myLoc为指向NSInteger(NSInteger *myLoc)的指针,而不是实际的NSInteger(NSInteger myLoc).它需要是后者.

1 个回答
  • 根据错误消息,您声明myLoc为指向NSInteger(NSInteger *myLoc)的指针,而不是实际的NSInteger(NSInteger myLoc).它需要是后者.

    2023-02-07 11:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有