gpt4 book ai didi

ios - 使用NSURL和NSJSON运行应用程序时出现“NSInvalidArgumentException”

转载 作者:行者123 更新时间:2023-12-01 19:15:08 26 4
gpt4 key购买 nike

我设计了一个应用程序,它将有一个按钮和三个标签。

当按下按钮时,将调用一个URL。 URL的JSON表示如下:

[
{
"_id": "50c87e8a1898044ea1458e4c",
"value": "10",
"time": "12:10",
"date": "12.12.12"
},
{
"_id": "50c87f311898044ea1458e4d",
"value": "12",
"time": "13:15",
"date": "12.12.12"
}
]

我的应用程序中还有三个标签,分别显示值,时间和日期。

我的应用程序的代码如下:
-(IBAction)getrequest:(id)sender
{
NSURL *url =[NSURL URLWithString:@"http://localhost:3000/lights"];
NSData* data = [NSData dataWithContentsOfURL:url];
//fetch the data to the JSON Foundation opject.
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
}

- (void)fetchedData:(NSData *)responseData {
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
[self processData:json];
}

-(void)processData:(NSDictionary *) JSONObject{
NSString *value = [JSONObject valueForKey:@"value"];
NSString *date = [JSONObject valueForKey:@"date"];
NSString *time = [JSONObject valueForKey:@"time"];


NSLog(@"value: %@", value);
NSLog(@"date: %@", date);
NSLog(@"time: %@", time);

_value.text = value;
_date.text = date;
_time.text = time;

}

代码可以编译并生成,没有任何错误,但是当我按下模拟器中的按钮时,出现如下异常:
2012-12-12 14:16:21.115 Sensor_visulization[7956:11303] value: (
10,
12
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] date: (
"12.12.12",
"12.12.12"
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] time: (
"12:10",
"13:15"
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7191a30
2012-12-12 14:16:21.117 Sensor_visulization[7956:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7191a30'

我的json解析器有问题吗?

我可以将所有值,时间和日期存储在数组中吗?

并在标签上显示数组中的最后一项?

任何帮助将非常感激。

最佳答案

当您的valueForKey对象是字典条目数组时,关于json的功能似乎存在普遍的困惑。 Midhun的答案通过仅向processData传递一个字典条目来解决该问题(这是一个逻辑解决方案,因为您原始代码的NSString *value声明显然暗示您只希望将一个字典条目传递给它)。

但是,如果要获取字典数组的所有三个字典键的数组,则可以使用原始的fetchedData,并对原始的valueForKey对象使用json来返回NSArray对象。为此,我建议对原始代码进行一些小的修改:

- (void)fetchedData:(NSData *)responseData {
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
[self processData:json];
}

-(void)processData:(NSDictionary *) JSONObject{

// get the three array structures

NSArray *value = [JSONObject valueForKey:@"value"];
NSArray *date = [JSONObject valueForKey:@"date"];
NSArray *time = [JSONObject valueForKey:@"time"];

// these log statements demonstrate that value, date, and time are arrays

NSLog(@"value: %@", value);
NSLog(@"date: %@", date);
NSLog(@"time: %@", time);

// now, let's grab the last object from each to populate our controls

_value.text = [value lastObject];
_date.text = [date lastObject];
_time.text = [time lastObject];
}

顺便说一句,在您的评论之一中,您在括号内引用了要绘制这些结果的图表。如果是这样,我可能会想像您需要将日期和时间组合到 NSDate对象中。如果希望将它们作为 NSDate对象,则可以:
-(void)processData:(NSDictionary *) JSONObject{

// get the values

NSArray *value = [JSONObject valueForKey:@"value"];

// get the datetimes

NSMutableArray *datetimes = [NSMutableArray array];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yy HH:mm";
formatter.locale = [NSLocale currentLocale];

for (NSDictionary *dictionary in JSONObject)
{
NSString *datetimeString = [NSString stringWithFormat:@"%@ %@", [dictionary objectForKey:@"date"], [dictionary objectForKey:@"time"]];
NSDate *datetime = [formatter dateFromString:datetimeString];
[datetimes addObject:datetime];
}

NSLog(@"values=%@", values);
NSLog(@"datetimes=%@", datetimes);

因为您的样本数据具有今天唯一的日期 12/12/12,所以我无法确定您的日期格式,所以我猜您正在使用 dd.MM.yy,但是显然使用对您的JSON数据有意义的任何日期格式字符串。

关于ios - 使用NSURL和NSJSON运行应用程序时出现“NSInvalidArgumentException”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13840835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com