使用NSInteger的作为数组索引 [英] Use NSInteger as array index

查看:816
本文介绍了使用NSInteger的作为数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个快乐的一年!

我有以下的麻烦。第一逻辑:

获取JSON从服务器。 JSON值始终是一个数。阅读的plist到数组。从服务器的JSON价值指数NSLog的数组。

我的plist:

 键类型值
项目0字符串纽约
项目1串亚特兰大

我的code:

获取的Json

 的NSString * url_req = @http://domain.com/index.php            *的NSURLRequest请求= [的NSURLRequest requestWithURL:[NSURL URLWithString:url_req]];            NSData的*响应= [NSURLConnection的sendSynchronousRequest:要求
                                                     returningResponse:无错误:无];            NSError * jsonParsingError =零;
            *的NSDictionary JSON = [NSJSONSerialization JSONObjectWithData:响应
                                                                      选项​​:0错误:&放大器; jsonParsingError]。            NSArray的* cityArray = [JSON objectForKey:@城市];
            对于(*的NSDictionary在pestoArray警报){
                myLoc = [[警报objectForKey:@位置] integerValue];
            }

JSON是工作就好了!

阅读plist中和的NSLog相应的值:

 的NSString *路径= [[一个NSBundle mainBundle] pathForResource:@townsListofType:@的plist];
 NSArray的* arrayNo = [[NSArray的页头] initWithContentsOfFile:路径];
 的NSLog(@城市%@,arrayNo [myLoc]);

到目前为止 arrayNo [myLoc] 将返回null,但 arrayNo [1] 将返回亚特兰大。

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

感谢您!

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

 索引前pression是无效的,因为标型'NSInteger的*'(又名'诠释*')不是一个整数或Objective-C的指针类型


解决方案

根据错误信息,您声明 myLoc 作为指向一个NSInteger( NSInteger的* myLoc ),而不是实际的NSInteger的( NSInteger的myLoc )。它需要后者。

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

解决方案

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.

这篇关于使用NSInteger的作为数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆