我使用 JSON 但有例外 [NSString objectForKey:]

发布于 2024-12-21 09:04:39 字数 843 浏览 1 评论 0原文

我在数据库中读取信息我使用 JSON 但有例外

'NSInvalidArgumentException',原因:'-[NSCFString objectForKey:]:无法识别的选择器发送到实例 0x603dfb0'

我认为这部分:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];

    NSArray *array = [str JSONValue];
    if (!array)
         return;

    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"yyyy-MM-dd"];

    for (NSDictionary *dict in array) {

         NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
         NSLog(@"%@",d);
         [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
    }
}

i read information in my database i use JSON but there is exception

'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x603dfb0'

I think this part :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];

    NSArray *array = [str JSONValue];
    if (!array)
         return;

    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"yyyy-MM-dd"];

    for (NSDictionary *dict in array) {

         NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
         NSLog(@"%@",d);
         [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-12-28 09:04:39

调整我的项目的构建设置以将此标志包含在“其他链接器标志”中

“向链接器传递一个特殊标志“-ObjC”。根据以下内容

http://beatworm.co.uk/blog/computers/using-categories-on-objective-c-classes-in-a-static-library/

编辑:
为什么要终止?

编辑2:
好吧,不太确定(因为我从来没有做过这种类型的编码),但是考虑到 json 数据:

{"event":[
  {"eve_date":"2011-12-18","eve_time":"00:00:06","title_event":"Google event","description":"Google event for IT student"},
  {"eve_date":"2011-12-29","eve_time":"00:00:08","title_event":"microsoft event","description":"microsoft event for IT student"}
]}

第一个对象是一个标记为“event”的数组,这可能会让你失望。

因此,尝试将其替换

for (NSDictionary *dict in array) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

为:

NSArray *array2 = [array objectForKey:@"event"];
for (NSDictionary *dict in array2) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

使用: 使用 JSONValue 获取 JSON 内容时出现问题

不确定这是否有效,但如果有效,对我来说会更有意义。

"pass the linker a special flag, ‘-ObjC’. Adjusting the build settings of my project to include this flag in the ‘Other linker flags’"

according to:

http://beatworm.co.uk/blog/computers/using-categories-on-objective-c-classes-in-a-static-library/

EDIT:
Why it is terminating?

EDIT2:
OK, not exactly sure (as I've never done this type of coding), but given the json data:

{"event":[
  {"eve_date":"2011-12-18","eve_time":"00:00:06","title_event":"Google event","description":"Google event for IT student"},
  {"eve_date":"2011-12-29","eve_time":"00:00:08","title_event":"microsoft event","description":"microsoft event for IT student"}
]}

The first object is an array labeled "event" which might be throwing you off.

So, try replacing this:

for (NSDictionary *dict in array) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

with:

NSArray *array2 = [array objectForKey:@"event"];
for (NSDictionary *dict in array2) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

using: Problem getting JSON contents with JSONValue

Not sure if that will work, but if it does, it will make more sense to me.

岛歌少女 2024-12-28 09:04:39

我通过更改 JSON 的位置文件类解决了异常,但该事件未

显示在日历中。

日历已显示,但不包含任何事件

提示:当我将 url 输入浏览器时,数据库显示的内容

I solve exception by change location files class of JSON but the event doesn't

display in calendar .

the calendr is display but didn't contain any event

hint: when i put url in browser the content of database display

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文