1

2つのステートメントの違いは何ですか

NSDate *today = [NSDate date];
NSDate *tomarow = [today dateByAddingTimeInterval:60*60*24];
NSDate *nextday = [NSDate dateWithTimeInterval:60*60*24 sinceDate:today];
4

1 に答える 1

5

2 つのメソッドの唯一の違いは、一方がクラス メソッドで、もう一方がインスタンス メソッドであることです。

次のコード スニペットは、両方の方法の使用方法を示しています。

// Today's Date
NSDate *today = [NSDate new];

// Date With Class Method
NSDate *tomorrow1 = [NSDate dateWithTimeInterval:60*60*24 sinceDate:today];
NSLog(@"Date from class method: %@", tomorrow1);

// Date With Instance Method
NSDate *tomorrow2 = [today dateByAddingTimeInterval:60*60*24];
NSLog(@"Date from instance method: %@", tomorrow2);

上記のコード スニペットは、次のような出力を提供します。

クラス メソッドからの日付: 2012-12-27 09:35:15 +0000

インスタンスメソッドからの日付: 2012-12-27 09:35:15 +0000

詳細については、NSDateを参照してください。

于 2012-12-26T09:38:00.020 に答える