NSNumber类

作者:追风剑情 发布于:2019-2-27 17:18 分类:Objective-C

NSNumber的创建方法和检索方法
创建和初始化方法 初始化实例方法 检索实例方法
numberWithChar: initWithChar: charValue
numberWithUnsignedChar: initWithUnsignedChar: unsignedCharValue
numberWithShort: initWithShort: shortValue
numberWithUnsignedShort: initWithUnsignedShort: unsignedShortValue
numberWithInteger: initWithInteger:
integerValue
numberWithUnsignedInteger: initWithUnsignedInteger:
unsignedIntegerValue
numberWithInt: initWithInt:
intValue
numberWithUnsignedInt: initWithUnsignedInt:
unsignedIntValue
numberWithLong: initWithLong:
longValue
numberWithUnsignedLong: initWithUnsignedLong:
unsignedLongValue
numberWithLongLong: initWithLongLong:
longlongValue
numberWithUnsignedLongLong: initWithUnsignedLongLong:
unsignedLongLongValue
numberWithFloat: initWithFloat:
floatValue
numberWithDouble: initWithDouble:
doubleValue
numberWithBool: initWithBool:
boolValue


注意:在Xcode 4.2之前的版本,使用哪个版本的方法是很重要的,有一类方法创建的对象是会自动释放的,然而使用alloc版本创建的对象需要在使用完后自己负责释放(在IOS中)。随着Objective-C中引入了自动引用计数(ARC),已经能够自动处理内存管理,就不再偏向于使用某一类方法。


示例一:

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. NSNumber *myNumber, *floatNumber, *intNumber;
  6. // NSInteger不是一个对象,而是基本数据类型的typedef。
  7. // 它实际上是64位的long或者32位的int。NSUInteger也是类似的typedef
  8. NSInteger myInt;
  9. // integer 型值
  10. intNumber = [NSNumber numberWithInteger: 100];
  11. myInt = [intNumber integerValue];
  12. NSLog(@"%li", (long) myInt);
  13. // long 型值
  14. myNumber = [NSNumber numberWithLong: 0xabcdef];
  15. NSLog(@"%lx", [myNumber longValue]);
  16. // char 型值
  17. myNumber = [NSNumber numberWithChar: 'X'];
  18. NSLog(@"%c", [myNumber charValue]);
  19. // float 型值
  20. floatNumber = [NSNumber numberWithFloat: 100.00];
  21. NSLog(@"%g", [floatNumber floatValue]);
  22. // double 型值
  23. myNumber = [NSNumber numberWithDouble: 12345e+15];
  24. NSLog(@"%lg", [myNumber doubleValue]);
  25. // 发生错误
  26. NSLog(@"%li", (long) [myNumber integerValue]);
  27. // 验证两个Number是否相等
  28. if ([intNumber isEqualToNumber: floatNumber] == YES)
  29. NSLog(@"Numbers are equal");
  30. else
  31. NSLog(@"Numbers are not equal");
  32. // 验证一个Number是否小于、等于或大于另一个Number
  33. // compare函数的返回值:
  34. // 小于 返回NSOrderedAscending
  35. // 等于 返回NSOrderdSame
  36. // 大于 返回NSOrderedDescending
  37. if ([intNumber compare: myNumber] == NSOrderedAscending)
  38. NSLog(@"First number is less than second");
  39. }
  40. return 0;
  41. }

运行测试

111.png

示例二:通过@表达式创建数字对象

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. NSNumber *myNumber, *floatNumber, *intNumber;
  6. NSInteger myInt;
  7. // 注意:如果在@之后的值是一个表达式或者变量需要使用括号。
  8. // NSDecimalNumber是NSNumber的子类,在对象层面提供了一些数字的四则运算方法。
  9. // 整型 integer
  10. intNumber = @100;
  11. myInt = [intNumber integerValue];
  12. NSLog(@"%li", (long) myInt);
  13. // 长整型 long integer
  14. myNumber = @0xabcdefL;
  15. NSLog(@"%lx", [myNumber longValue]);
  16. // 字符型 char
  17. myNumber = @'X';
  18. NSLog(@"%c", [myNumber charValue]);
  19. // 浮点型 float
  20. floatNumber = @100.0f;
  21. NSLog(@"%g", [floatNumber floatValue]);
  22. }
  23. return 0;
  24. }

运行测试
111.png


标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号