【问题标题】:throw exception [__NSDate length]: unrecognized selector抛出异常 [__NSDate 长度]:无法识别的选择器
【发布时间】:2011-12-25 22:26:30
【问题描述】:

这是我的代码:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"str: %@",str);

NSDictionary *dict = [str JSONValue];

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



NSArray *array = [[dict objectForKey:@"event"] retain];

NSLog(@"Array: %@",array);



for (NSDictionary *tempdict in array)
{


    NSDate *d = [fmt dateFromString:[tempdict objectForKey:@"eve_date"]];
    NSLog(@"Date %@",d);
    NSLog(@"Date of event %@",[tempdict objectForKey:@"eve_date"]);

    NSDate *t =[tempdict objectForKey:@"eve_date"];
            NSLog(@"Date of t %@",t);
    NSLog(@"This is title_event %@",[tempdict objectForKey:@"title"]);
    NSLog (@"Time of event %@", [tempdict objectForKey:@"eve_time"]);
    NSLog(@"This is description %@",[tempdict objectForKey:@"description"]);

[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:t]];}
dataReady = YES;
[callback loadedDataSource:self];}



+ (Events*)eventsNamed:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate {

return [[[Events alloc] initWithName:atitle description:adescription date:aDate] autorelease]; }

打印我所有的数据都很好,但在这一行

    [eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:t]];

有例外:

** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSDate 长度]:无法识别的选择器发送到实例 0x6149cb0”

【问题讨论】:

  • eventPHP 是什么类?是 NSArray 吗?
  • 请在事件上发布 initWithName:atitle description:adescription date:aDate 方法
  • 代码示例应该完整简洁。请提供minimal sample。当前示例包含无关代码。
  • Mike K : eventPHP 它是数组 [ NSMutableArray ] Dimitar - (id)initWithName:(NSString *)atitle description:(NSString *)description date:(NSDate *)aDate { if ((self = [超级初始化])) { 标题 = [标题副本];描述 = [描述副本];日期 = [aDate 保留]; } return self;} initWithName 有什么问题吗?

标签: iphone xcode nsdate


【解决方案1】:

您将length 消息发送到NSDate 对象,但NSDate 对象不理解length 消息。您没有向我们展示发送该消息的代码。

如果您在objc_exception_throw 上设置断点,当异常发生时,Xcode 会在调试器中停止您的应用程序,因此您可以准确查看length 消息的发送位置。

【讨论】:

  • 是的,我在 initWhithName 中将它与 NSLog 一起使用 return self 有异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多