2012-12-26 28 views
1

就是兩種說法差(NSTimeInterval)

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

你發現了什麼? –

+0

@InderKumarRathore - 我得到這兩個陳述相同的價值。它們之間的差別是什麼 – ray1th

+1

@ ray1th檢查我的答案! –

回答

5

兩種方法之間的唯一區別是,之間的區別一個是類方法,另一個是實例方法。

以下代碼段演示使用的兩種方法:

// 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日9點35分15秒0000

欲瞭解更多信息,請參閱NSDate

+0

你可以通過傳遞負值相同的'-60 * 60 * 24' –

+0

@InderKumarRathore和這裏我們去:tomarrow 2012-12-25 09:46:50 +0000,nextday 2012-12-25 09:46: 50 +0000!怎麼了? –

+1

沒有錯,我現在在窗戶上,所以無法檢查。這就是我告訴你的原因。無論如何+1 f –