Advertisements
NSNumber by example
Creating an NSNumber Object
+ numberWithBool:
Creates and returns an NSNumber object containing a given value, treating it as a BOOL.
Example
NSNumber *number = [NSNumber numberWithBool:YES]; NSLog(@"%@",number);
Output
2014-04-16 04:54:14.652 iOS-Tutorial[562:a0b] 1
+ numberWithChar:
Creates and returns an NSNumber object containing a given value, treating it as a signed char.
Example
NSNumber *number = [NSNumber numberWithChar:'t']; NSLog(@"%@",number);
Output
2014-04-16 04:55:22.045 iOS-Tutorial[577:a0b] 116
+ numberWithDouble:
Creates and returns an NSNumber object containing a given value, treating it as a double.
Example
NSNumber *number = [NSNumber numberWithDouble:20.89]; NSLog(@"%@",number);
Output
2014-04-16 04:55:53.473 iOS-Tutorial[590:a0b] 20.89
+ numberWithFloat:
Creates and returns an NSNumber object containing a given value, treating it as a float.
Example
NSNumber *number = [NSNumber numberWithFloat:20.89]; NSLog(@"%@",number);
Output
2014-04-16 04:56:25.757 iOS-Tutorial[602:a0b] 20.89
+ numberWithInt:
Creates and returns an NSNumber object containing a given value, treating it as a signed int.
Example
NSNumber *number = [NSNumber numberWithInt:20]; NSLog(@"%@",number);
Output
2014-04-16 04:56:56.020 iOS-Tutorial[614:a0b] 20
+ numberWithInteger:
Creates and returns an NSNumber object containing a given value, treating it as an NSInteger.
Example
NSNumber *number = [NSNumber numberWithInteger:20]; NSLog(@"%@",number);
Output
2014-04-16 04:57:23.211 iOS-Tutorial[626:a0b] 20
+ numberWithLong:
Creates and returns an NSNumber object containing a given value, treating it as a signed long.
Example
NSNumber *number = [NSNumber numberWithLong:20]; NSLog(@"%@",number);
Output
2014-04-16 04:57:52.890 iOS-Tutorial[638:a0b] 20
+ numberWithLongLong:
Creates and returns an NSNumber object containing a given value, treating it as a signed long long.
Example
NSNumber *number = [NSNumber numberWithLongLong:20]; NSLog(@"%@",number);
Output
2014-04-16 04:58:18.213 iOS-Tutorial[652:a0b] 20
+ numberWithShort:
Creates and returns an NSNumber object containing value, treating it as a signed short.
Example
NSNumber *number = [NSNumber numberWithShort:20]; NSLog(@"%@",number);
Output
2014-04-16 04:58:38.256 iOS-Tutorial[664:a0b] 20
+ numberWithUnsignedChar:
Creates and returns an NSNumber object containing a given value, treating it as an unsigned char.
Example
NSNumber *number = [NSNumber numberWithUnsignedChar:'a']; NSLog(@"%@",number);
Output
2014-04-16 04:59:19.390 iOS-Tutorial[678:a0b] 97
+ numberWithUnsignedInt:
Creates and returns an NSNumber object containing a given value, treating it as an unsigned int.
Example
NSNumber *number = [NSNumber numberWithUnsignedInt:23]; NSLog(@"%@",number];
Output
2014-04-16 05:00:54.791 iOS-Tutorial[695:a0b] 23
+ numberWithUnsignedInteger:
Creates and returns an NSNumber object containing a given value, treating it as an NSUInteger.
Example
NSNumber *number = [NSNumber numberWithUnsignedInteger:23]; NSLog(@"%@",number);
Output
2014-04-16 05:01:26.508 iOS-Tutorial[708:a0b] 23
+ numberWithUnsignedLong:
Creates and returns an NSNumber object containing a given value, treating it as an unsigned long.
Example
NSNumber *number = [NSNumber numberWithUnsignedLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:01:54.248 iOS-Tutorial[720:a0b] 23
+ numberWithUnsignedLongLong:
Creates and returns an NSNumber object containing a given value, treating it as an unsigned long long.
Example
NSNumber *number = [NSNumber numberWithUnsignedLongLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:02:17.541 iOS-Tutorial[732:a0b] 23
+ numberWithUnsignedShort:
Creates and returns an NSNumber object containing a given value, treating it as an unsigned short.
Example
NSNumber *number = [NSNumber numberWithUnsignedShort:23]; NSLog(@"%@",number);
Output
2014-04-16 05:02:44.967 iOS-Tutorial[744:a0b] 23
Initializing an NSNumber Object
- initWithBool:
Returns an NSNumber object initialized to contain a given value, treated as a BOOL.
Example
NSNumber *number = [[NSNumber alloc]initWithBool:23]; NSLog(@"%@",number);
Output
2014-04-16 05:03:45.192 iOS-Tutorial[759:a0b] 1
- initWithChar:
Returns an NSNumber object initialized to contain a given value, treated as a signed char.
Example
NSNumber *number = [[NSNumber alloc]initWithChar:'a']; NSLog(@"%@",number);
Output
2014-04-16 05:04:20.052 iOS-Tutorial[771:a0b] 97
- initWithDouble:
Returns an NSNumber object initialized to contain value, treated as a double.
Example
NSNumber *number = [[NSNumber alloc]initWithDouble:23.87]; NSLog(@"%@",number);
Output
2014-04-16 05:04:54.547 iOS-Tutorial[785:a0b] 23.87
- initWithFloat:
Returns an NSNumber object initialized to contain a given value, treated as a float.
Example
NSNumber *number = [[NSNumber alloc]initWithFloat:23.87]; NSLog(@"%@",number);
Output
2014-04-16 05:05:19.591 iOS-Tutorial[797:a0b] 23.87
- initWithInt:
Returns an NSNumber object initialized to contain a given value, treated as a signed int.
Example
NSNumber *number = [[NSNumber alloc]initWithInt:23]; NSLog(@"%@",number);
Output
2014-04-16 05:05:47.535 iOS-Tutorial[809:a0b] 23
- initWithInteger:
Returns an NSNumber object initialized to contain a given value, treated as an NSInteger.
Example
NSNumber *number = [[NSNumber alloc]initWithInteger:23]; NSLog(@"%@",number);
Output
2014-04-16 05:06:10.067 iOS-Tutorial[821:a0b] 23
- initWithLong:
Returns an NSNumber object initialized to contain a given value, treated as a signed long.
Example
NSNumber *number = [[NSNumber alloc]initWithLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:06:38.763 iOS-Tutorial[833:a0b] 23
- initWithLongLong:
Returns an NSNumber object initialized to contain value, treated as a signed long long.
Example
NSNumber *number = [[NSNumber alloc]initWithLongLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:07:51.488 iOS-Tutorial[848:a0b] 23
- initWithShort:
Returns an NSNumber object initialized to contain a given value, treated as a signed short.
Example
NSNumber *number = [[NSNumber alloc]initWithShort:23]; NSLog(@"%@",number);
Output
2014-04-16 05:08:17.614 iOS-Tutorial[860:a0b] 23
- initWithUnsignedChar:
Returns an NSNumber object initialized to contain a given value, treated as an unsigned char.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedChar:'a']; NSLog(@"%@",number);
Output
2014-04-16 05:08:47.289 iOS-Tutorial[872:a0b] 97
- initWithUnsignedInt:
Returns an NSNumber object initialized to contain a given value, treated as an unsigned int.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedInt:23]; NSLog(@"%@",number);
Output
2014-04-16 05:09:18.065 iOS-Tutorial[884:a0b] 23
- initWithUnsignedInteger:
Returns an NSNumber object initialized to contain a given value, treated as an NSUInteger.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedInteger:23]; NSLog(@"%@",number);
Output
2014-04-16 05:09:41.609 iOS-Tutorial[896:a0b] 23
- initWithUnsignedLong:
Returns an NSNumber object initialized to contain a given value, treated as an unsigned long.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:10:09.807 iOS-Tutorial[908:a0b] 23
- initWithUnsignedLongLong:
Returns an NSNumber object initialized to contain a given value, treated as an unsigned long long.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedLongLong:23]; NSLog(@"%@",number);
Output
2014-04-16 05:10:37.393 iOS-Tutorial[920:a0b] 23
- initWithUnsignedShort:
Returns an NSNumber object initialized to contain a given value, treated as an unsigned short.
Example
NSNumber *number = [[NSNumber alloc]initWithUnsignedShort:23]; NSLog(@"%@",number);
Output
2014-04-16 05:10:37.393 iOS-Tutorial[920:a0b] 23
Accessing Numeric Values
- boolValue
Returns the receivers value as a BOOL.
Example
NSNumber *number = [NSNumber numberWithBool:YES]; NSLog(@"%d",[number boolValue]);
Output
2014-04-16 05:11:59.626 iOS-Tutorial[936:a0b] 1
- charValue
Returns the receivers value as a char.
Example
NSNumber *number = [NSNumber numberWithChar:'A']; NSLog(@"%c",[number charValue]);
Output
2014-04-16 05:12:59.850 iOS-Tutorial[969:a0b] A
- decimalValue
Returns the receivers value, expressed as an NSDecimal structure.
Example
NSNumber *number = [NSNumber numberWithDouble:10.2323]; NSLog(@"%d %d %d %d %d %d",[number decimalValue]._exponent,[number decimalValue]._isCompact,[number decimalValue]._isNegative,[number decimalValue]._length,[number decimalValue]._reserved,[number decimalValue]._mantissa[0]);
Output
2014-04-16 05:17:25.052 iOS-Tutorial[1000:a0b] -4 1 0 2 0 36787
- doubleValue
Returns the receivers value as a double.
Example
NSNumber *number = [NSNumber numberWithDouble:10.2323]; NSLog(@"%f",[number doubleValue]);
Output
2014-04-16 05:18:32.673 iOS-Tutorial[1021:a0b] 10.232300
- floatValue
Returns the receivers value as a float.
Example
NSNumber *number = [NSNumber numberWithDouble:10.2323]; NSLog(@"%f",[number floatValue]);
Output
2014-04-16 05:18:56.488 iOS-Tutorial[1033:a0b] 10.232300
- intValue
Returns the receivers value as an int.
Example
NSNumber *number = [NSNumber numberWithInteger:12]; NSLog(@"%d",[number intValue]);
Output
2014-04-16 05:19:50.368 iOS-Tutorial[1045:a0b] 12
- integerValue
Returns the receivers value as an NSInteger.
Example
NSNumber *number = [NSNumber numberWithInteger:12]; NSLog(@"%d",[number integerValue]);
Output
2014-04-16 05:20:20.704 iOS-Tutorial[1057:a0b] 12
- longLongValue
Returns the receivers value as a long long.
Example
NSNumber *number = [NSNumber numberWithLongLong:12]; NSLog(@"%lld",[number longLongValue]);
Output
2014-04-16 05:21:19.141 iOS-Tutorial[1069:a0b] 12
- longValue
Returns the receivers value as a long.
Example
NSNumber *number = [NSNumber numberWithLong:12]; NSLog(@"%ld",[number longValue]);
Output
2014-04-16 05:21:53.138 iOS-Tutorial[1082:a0b] 12
- shortValue
Returns the receivers value as a short.
Example
NSNumber *number = [NSNumber numberWithShort:12]; NSLog(@"%d",[number shortValue]);
Output
2014-04-16 05:22:24.625 iOS-Tutorial[1094:a0b] 12
- unsignedCharValue
Returns the receivers value as an unsigned char.
Example
NSNumber *number = [NSNumber numberWithUnsignedChar:'a']; NSLog(@"%c",[number unsignedCharValue]);
Output
2014-04-16 05:23:35.698 iOS-Tutorial[1108:a0b] a
- unsignedIntegerValue
Returns the receivers value as an NSUInteger.
Example
NSNumber *number = [NSNumber numberWithUnsignedInteger:12]; NSLog(@"%d",[number unsignedIntegerValue]);
Output
2014-04-16 05:24:38.770 iOS-Tutorial[1123:a0b] 12
- unsignedIntValue
Returns the receivers value as an unsigned int.
Example
NSNumber *number = [NSNumber numberWithUnsignedInteger:12]; NSLog(@"%d",[number unsignedIntValue]);
Output
2014-04-16 05:25:02.346 iOS-Tutorial[1136:a0b] 12
- unsignedLongLongValue
Returns the receivers value as an unsigned long long.
Example
NSNumber *number = [NSNumber numberWithUnsignedLongLong:12]; NSLog(@"%llu",[number unsignedLongLongValue]);
Output
2014-04-16 05:25:39.445 iOS-Tutorial[1148:a0b] 12
- unsignedLongValue
Returns the receivers value as an unsigned long.
Example
NSNumber *number = [NSNumber numberWithUnsignedLong:12]; NSLog(@"%lu",[number unsignedLongValue]);
Output
2014-04-16 05:26:11.371 iOS-Tutorial[1160:a0b] 12
- unsignedShortValue
Returns the receivers value as an unsigned short.
Example
NSNumber *number = [NSNumber numberWithUnsignedShort:12]; NSLog(@"%u",[number unsignedShortValue]);
Output
2014-04-16 05:26:56.966 iOS-Tutorial[1172:a0b] 12
Retrieving String Representations
- descriptionWithLocale:
Returns a string that represents the contents of the receiver for a given locale.
Example
NSNumber *number = [NSNumber numberWithUnsignedShort:12]; NSLog(@"%@",[number descriptionWithLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]]);
Output
2014-04-16 05:28:39.951 iOS-Tutorial[1186:a0b] 12
- stringValue
Returns the receivers value as a human-readable string.
Example
NSNumber *number = [NSNumber numberWithUnsignedShort:12]; NSLog(@"%@",[number stringValue]);
Output
2014-04-16 05:29:11.144 iOS-Tutorial[1199:a0b] 12
Comparing NSNumber Objects
- compare:
Returns an NSComparisonResult value that indicates whether the receiver is greater than, equal to, or less than a given number.
Example
NSNumber *number1 = [NSNumber numberWithUnsignedShort:12]; NSNumber *number2 = [NSNumber numberWithFloat:12.3434]; NSComparisonResult result = [number2 compare:number1]; NSLog(@"%d",result);
Output
2014-04-16 05:30:30.629 iOS-Tutorial[1214:a0b] 1
- isEqualToNumber:
Returns a Boolean value that indicates whether the receiver and a given number are equal.
Example
NSNumber *number1 = [NSNumber numberWithUnsignedShort:12]; NSNumber *number2 = [NSNumber numberWithFloat:12.3434]; NSComparisonResult result = [number2 isEqualToNumber:number1]; NSLog(@"%d",result);
Output
2014-04-16 05:31:03.955 iOS-Tutorial[1227:a0b] 0
Accessing Type Information
- objCType
Returns a C string containing the Objective-C type of the data contained in the receiver.
Example
NSNumber *number = [NSNumber numberWithUnsignedShort:12]; NSLog(@"%s",[number objCType])
Output
2014-04-16 05:31:41.950 iOS-Tutorial[1239:a0b] i
Advertisements