0

私は次のものを持っていますNSString

NSString * dateString = @"2012-01-24T14:59:01Z";

その文字列からを作成したいNSDateNSDateクラスリファレンスを調べて、使用することを考えましたdateWithNaturalLanguageString:

NSDate指定された文字列で指定された日付と時刻に設定されたオブジェクトを作成して返します。

+ (id)dateWithNaturalLanguageString:(NSString *)string

パラメータ string 「先週の火曜日の夕食」、「2001 年 12 月 31 日午後 3 時」、「12/31/01」、「31/12/01」など、口語的な日付指定を含む文字列。 戻り値NSDate文字列で指定された現在の日付と時刻に設定された 新しいオブジェクト。

ただし、次のように使用しようとすると:

NSDate * date = [NSDate dateWithNaturalLanguageString:dateString];

次のエラーが表示されます。

セレクター「dateWithNaturalLanguageString:」の既知のクラス メソッドはありません

4

4 に答える 4

4

NSDateFormatterクラスは、この問題を解決するのに役立ちます。そして、これについてはすでに多くの質問があります。たとえば、最初の質問は次のとおりです。Convert NSString-> NSDate?

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

于 2012-01-24T16:05:07.190 に答える
1

コンストラクターコンストラクターを使用してみてください。dateWithStringまたは(同様のエラーが発生する場合は)、ここでNSDateFormatter説明されているようにを使用してみてください。

于 2012-01-24T16:06:28.487 に答える
1

クラスメソッドは、dateWithNaturalLanguageString:iOS ではなく Mac OS X でのみ実装されているため、エラーが発生しています。

探しているものを達成するには、NSDateFormatterクラスが必要です。このクラスは非常に重いため、最初にドキュメントを読んで最適な使用方法を理解する必要があります。

于 2012-01-24T16:07:20.250 に答える
1

ここで探していたものが見つかりました。これは RFC 3339 の日時です。

- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
    // Returns a user-visible date time string that corresponds to the
    // specified RFC 3339 date time string. Note that this does not handle
    // all possible RFC 3339 date time strings, just one of the most common
    // styles.
{
    NSString *          userVisibleDateTimeString;
    NSDateFormatter *   rfc3339DateFormatter;
    NSLocale *          enUSPOSIXLocale;
    NSDate *            date;
    NSDateFormatter *   userVisibleDateFormatter;

    userVisibleDateTimeString = nil;

    // Convert the RFC 3339 date time string to an NSDate.

    rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];

    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];
    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
    if (date != nil) {

        // Convert the NSDate to a user-visible date string.

        userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        assert(userVisibleDateFormatter != nil);

        [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
        [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

        userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
    }
    return userVisibleDateTimeString;
}
于 2012-01-24T16:18:00.867 に答える