'NSDate'没有可见的@interface声明选择器'initWithString:' [英] No visible @interface for 'NSDate' declares the selector 'initWithString:'
问题描述
可能重复:
NSDate initWithString
我写了以下代码行:
[[NSDate alloc] initWithString:@"2013-03-24 10:45:32 +0200"];
当我尝试编译时,我收到以下错误消息:
When I try to compile I obtain the following error message:
ARC语义问题:
'NSDate'没有可见的@interface声明选择器'initWithString:'
ARC Semantic Issue: No visible @interface for 'NSDate' declares the selector 'initWithString:'
在项目中有以下包括:
#import <Foundation/Foundation.h>
为了确保包含该类,我尝试了以下编译的行,没有问题:
To be sure that class is included, I tried following line which is compiled without issues:
[[NSDate alloc] init];
我的代码出了什么问题?
What is wrong with my code?
XCode版本4.6。
项目iOS 6.1中的基础SDK。
XCode version 4.6.
Base SDK in project iOS 6.1.
推荐答案
initWithString函数不存在于iOS平台的NSDate类,因为您需要首先使用NSDateFormatter指定日期格式化程序,它将日期和时间的文本表示转换为NSDate对象。使用类似这样的东西:
initWithString function is not present in NSDate class for iOS Platforms, since you'll need to first specify a date formatter using NSDateFormatter which converts textual representations of dates and times into NSDate objects. Use something like this :
NSString *currentDateString = @"2013-03-24 10:45:32";
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
NSDate *currentDate = [dateFormater dateFromString:currentDateString];
这篇关于'NSDate'没有可见的@interface声明选择器'initWithString:'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!