? ? ? ? 前段時(shí)間做了關(guān)于喂食計(jì)劃方面的APP徒坡,里面時(shí)間的設(shè)定用到了時(shí)間選擇器潮秘,于是在UIPickView的基礎(chǔ)上重寫了選擇器殴俱,遇到了最大時(shí)間和最小時(shí)間的問(wèn)題汽抚。下面總結(jié)一下NSDate的比較抓狭。
? ? 這里先要介紹NSDate的方法:- (NSComparisonResult)compare:(NSDate *)other,這是2個(gè)date比較大小的方法造烁,有個(gè)返回值NSComparisonResult類型否过,這是個(gè)枚舉類型數(shù)據(jù),其定義如下
??:NSComparisonResultresult = [aDate compare:bDate];
? ??typedef?NS_CLOSED_ENUM(NSInteger, NSComparisonResult) {
? ? NSOrderedAscending = -1L,???????? //?aDate < bDate
? ? NSOrderedSame, ? ? ? ? ? ? ? ? ? ?????????//?aDate == bDate
? ? NSOrderedDescending?????????????????//?aDate > bDate
};
? ? 當(dāng)然compare也不僅僅適用于date大小的比較惭蟋,同樣可以比較NSString苗桂、NSNumber等相關(guān)類型的大小比較,如:[@"a" compare:@"b"]告组,字符串的大小比較主要會(huì)根據(jù)字符串中的字符的ASCII碼逐個(gè)進(jìn)行比較煤伟。
下面貼上我寫的代碼
//比較2個(gè)date的大小 a>b = -1;? a=b = 0; a
+ (NSInteger )compareDate:(NSDate* )aDate withDate:(NSDate* )bDate
{
? ? NSInteger tag = 0;
? ? NSComparisonResult result = [aDate compare:bDate];
? ? if(result == NSOrderedSame)
? ? {
? ? ? ? //相等
? ? ? ? tag=0;
? ? }
? ? else if (result == NSOrderedAscending)
? ? {
? ? ? ? //bDate比aDate大
? ? ? ? tag = 1;
? ? }
? ? else
? ? {
? ? ? ? //bDate比aDate小
? ? ? ? tag = -1;
? ? }
? ? return?tag;
}
? ? 時(shí)間倉(cāng)促,錯(cuò)漏之處敬請(qǐng)指出木缝,謝謝便锨!
ps:貼上小弟畫的時(shí)間選擇器