1
我是iOS新手,在編寫方法時遇到一些問題需要遵循iOS模式。我試圖找到一個簡單的方法來使用objective-c在日期中增加值。操作日期
考慮:
NSInteger incrementType = 1; // from 1 to 4, days, weeks, months, year
NSInteger incrementSize = 20 // the increment size
NSDate* date = ... // some date
+(NSDate*)somename:(NSInteger)incrementSize type:(NSInteger)incrementType current:(NSDate*)date {
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* ateComponents = [[NSDateComponents alloc] init];
// switch
[weekdayComponents setMonth:incrementSize];
NSDate* newDate = [gregorian dateByAddingComponents:dateComponents toDate:date options:0];
return newDate;
}
問題:
- 我不知道如果邏輯是正確的。我在stackoverflow中找到了一段代碼,我試圖修改它。
- 如何爲incrementType參數編寫枚舉?
- 什麼是一個好的方法簽名?
我該如何使用它? 'NSDate * date = [NSDate date]; NSDate * newDate = [日期addDays:5];'? – lolol
@lolol:是的,這就是要走的路! –