標籤:ios開發 nsnumber
// 聊聊NSNumber, NSNumber可以封裝c中基礎資料型別 (Elementary Data Type) char,int,long,float,BOOL,NSInteger,CGFloat等。 NSNumber *num = [NSNumber numberWithChar:'a']; NSLog(@"%@", num); // 97 num = [NSNumber numberWithInteger:97]; num = [NSNumber numberWithFloat:97.0]; num = [NSNumber numberWithBool:YES]; // 不過直接用下面的方式封裝會更方便 num = @'a'; num = @97; num = @97.0; num = @YES; // 既然NSNumber能封裝BOOL,NSInteger,CGFloat等基礎資料格式,那如何判斷NSNumber是什麼基礎類型呢,接著看 /* + (NSNumber *)numberWithChar:(char)value; + (NSNumber *)numberWithUnsignedChar:(unsigned char)value; + (NSNumber *)numberWithShort:(short)value; + (NSNumber *)numberWithUnsignedShort:(unsigned short)value; + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithUnsignedInt:(unsigned int)value; + (NSNumber *)numberWithLong:(long)value; + (NSNumber *)numberWithUnsignedLong:(unsigned long)value; + (NSNumber *)numberWithLongLong:(long long)value; + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; + (NSNumber *)numberWithFloat:(float)value; + (NSNumber *)numberWithDouble:(double)value; + (NSNumber *)numberWithBool:(BOOL)value; + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0); + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0); */ num = [NSNumber numberWithChar:'a']; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber c num = [NSNumber numberWithUnsignedChar:'a']; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s num = [NSNumber numberWithShort:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s num = [NSNumber numberWithUnsignedShort:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i num = [NSNumber numberWithInt:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i num = [NSNumber numberWithUnsignedInt:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q num = [NSNumber numberWithLong:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i num = [NSNumber numberWithUnsignedLong:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q num = [NSNumber numberWithLongLong:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q num = [NSNumber numberWithUnsignedLongLong:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q num = [NSNumber numberWithFloat:1.2]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber f num = [NSNumber numberWithDouble:1.2]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber d num = [NSNumber numberWithBool:YES]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFBoolean c num = [NSNumber numberWithInteger:97]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i num = [NSNumber numberWithUnsignedInteger:YES]; NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i // 這樣就看出來了,判斷整型int類,包括short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long,就這樣判斷 NSArray *intTypeArray = @[@"s", @"i", @"q"]; NSString *intType = [NSString stringWithFormat:@"%s", num.objCType]; if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [intTypeArray containsObject:intType]) { NSLog(@"%@是一個整數", num); } // 浮點型判斷 NSArray *floatTypeArray = @[@"f", @"d"]; <pre name="code" class="objc"> NSString *floatType = [NSString stringWithFormat:@"%s", num.objCType]; if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [floatTypeArray containsObject:floatType]) { NSLog(@"%@是一個浮點型", num); } // char判斷 NSArray *charTypeArray = @[@"c"]; NSString *charType = [NSString stringWithFormat:@"%s", num.objCType]; if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [charTypeArray containsObject:charType]) { NSLog(@"%@是一個字元型", num); } // BOOL判斷 if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFBoolean"]) { NSLog(@"%@是一個字元型", num); } if ([num isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) { NSLog(@"num is BOOL type"); } else { NSLog(@"num is not BOOL type"); } // 下面聊NSNumberFormatter // 從學號談起,一般學號的全格式是 入學年份(前兩位)+大學(學院)代碼+專業代碼+班級+個人班級學號 05+01+02+1+01,給班級學生列成績單,知道學生班級學號,產生學校學號,如5號,應該產生050102105,我們想到的方法是c中的格式化,例如: NSInteger classCode = 5; NSString *schoolStuCode = [NSString stringWithFormat:@"0501021%2.2d", classCode]; NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105 // 另一種方法是NSNumberFormatter轉換,上代碼 // 資料格式轉換類 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; numberFormatter.numberStyle = kCFNumberFormatterNoStyle; numberFormatter.formatWidth = 2; // 資料寬度為2,不足前面補0 numberFormatter.paddingCharacter = @"0"; schoolStuCode = [NSString stringWithFormat:@"0501021%@", [numberFormatter stringFromNumber:@(classCode)]]; NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105 // 這裡也輸出了050102105 // 之後體驗NSNumberFormatter強大功能 // 字串分隔,可用於手機號顯示 NSString *formatterStr; numberFormatter.numberStyle = kCFNumberFormatterDecimalStyle; formatterStr = [numberFormatter stringFromNumber:@102140302]; NSLog(@"%@", formatterStr); // 102,140,302 // 從格式字串轉換為資料,其他類似 NSNumber *number = [numberFormatter numberFromString:formatterStr]; NSLog(@"number = %@", number); // 貨幣符號 numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; formatterStr = [numberFormatter stringFromNumber:@1.2]; NSLog(@"%@", formatterStr); // $1.20 // 百分數格式 numberFormatter.numberStyle = kCFNumberFormatterPercentStyle; formatterStr = [numberFormatter stringFromNumber:@1.2]; NSLog(@"%@", formatterStr); // 120% // 科學計數格式 numberFormatter.numberStyle = kCFNumberFormatterScientificStyle; formatterStr = [numberFormatter stringFromNumber:@1.2]; NSLog(@"%@", formatterStr); // 1.2E0 // 口語格式 numberFormatter.numberStyle = kCFNumberFormatterSpellOutStyle; formatterStr = [numberFormatter stringFromNumber:@1.2]; NSLog(@"%@", formatterStr); // one point two
objective-c 中資料類型之五 數值類(NSNumber及NSNumberFormatter)