xcode 'NSDate' 没有可见的 @interface 声明了选择器 'initWithString:'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14702626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:43:18  来源:igfitidea点击:

No visible @interface for 'NSDate' declares the selector 'initWithString:'

iosobjective-cxcode

提问by sergtk

Possible Duplicate:
NSDate initWithString

可能重复:
NSDate initWithString

I wrote the following line of code:

我写了以下代码行:

[[NSDate alloc] initWithString:@"2013-03-24 10:45:32 +0200"];

When I try to compile I obtain the following error message:

当我尝试编译时,我收到以下错误消息:

ARC Semantic Issue: No visible @interface for 'NSDate' declares the selector 'initWithString:'

ARC 语义问题:“NSDate”没有可见的 @interface 声明选择器“initWithString:”

In project there is following include:

在项目中有以下内容:

#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 version 4.6.
Base SDK in project iOS 6.1.

XCode 4.6 版。
项目 iOS 6.1 中的基础 SDK。

回答by Dhruv Goel

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 :

initWithString 函数在 iOS 平台的 NSDate 类中不存在,因为您需要首先使用 NSDateFormatter 指定一个日期格式化程序,它将日期和时间的文本表示形式转换为 NSDate 对象。使用这样的东西:

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];