NSDateFormatter 不兼容类型错误
我在 Xcode 中收到以下错误:
警告:不兼容的 Objective-C 类型 'struct NSDate *',在从不同的 Objective-C 类型传递'setUpdate:'的参数 1 时预期'struct NSString *'
当我尝试时发生错误将格式化字符串保存到 myObj.update
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
//myObj is an object with instance variable 'update' as a NSString string
myObj.update = [formatter dateFromString:@"2011-03-17T18:15:05Z"];
[formatter release];
我知道我做错了一些小错误,但无法查明。感谢您的帮助! :)
I am receiving the following error in Xcode:
warning: incompatible Objective-C types 'struct NSDate *', expected 'struct NSString *' when passing argument 1 of 'setUpdate:' from distinct Objective-C type
The error happens when I am trying to save the formatted string to myObj.update
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
//myObj is an object with instance variable 'update' as a NSString string
myObj.update = [formatter dateFromString:@"2011-03-17T18:15:05Z"];
[formatter release];
I know I am doing something wrong that is minor but can't pinpoint. Thanks for the help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dateFromString:
返回一个NSDate
实例,而不是字符串。您需要将您的update
属性设置为NSDate
。编译器基本上告诉您该方法正在返回一个日期,但您试图将其分配给字符串属性,而这并不健康!dateFromString:
returns an instance ofNSDate
, not a string. You need to make yourupdate
property anNSDate
. The compiler is basically telling you that the method is returning a date, but you're trying to assign that to a string property, and that's just not gonna be healthy!