DAY 5
0

## Day08 - Numbers

``````+ (NSNumber *)numberWithBool:(BOOL)value
``````

``````+ (NSNumber *)numberWithChar:(char)value         //2
+ (NSNumber *)numberWithDouble:(double)value     //3
+ (NSNumber *)numberWithFloat:(float)value       //4
+ (NSNumber *)numberWithInt:(int)value           //5
+ (NSNumber *)numberWithInteger:(NSInteger)value //6
``````
1. 建立並返回一個包含了給定值的`NSNumber`實體，將其視為`char`
2. 建立並返回一個包含了給定值的`NSNumber`實體，將其視為`double`
3. 建立並返回一個包含了給定值的`NSNumber`實體，將其視為`float`
4. 建立並返回一個包含了給定值的`NSNumber`實體，將其視為`int`
5. 建立並返回一個包含了給定值的`NSNumber`實體，將其視為`NSInteger`
``````- (BOOL)boolValue         //7
- (char)charValue         //8
- (double)doubleValue     //9
- (float)floatValue       //10
- (NSInteger)integerValue //11
- (int)intValue           //12
- (NSString *)stringValue //13
``````
1. 返回`BOOL`
2. 返回`char`
3. 返回`double`
4. 返回`float`
5. 返回`NSInteger`
6. 返回`int`
7. 返回`NSString`

ex.

``````#import <Foundation/Foundation.h>

@interface SampleClass : NSObject

- (NSNumber *)addA: (NSNumber *)a withB: (NSNumber *)b;

@end

@implementation SampleClass

- (NSNumber *)addA:(NSNumber *)a withB:(NSNumber *)b {
float number1 = [a floatValue]; // number1 = 類型為 float 的 a 值
float number2 = [b floatValue];
float value = number1 + number2;
NSNumber *result = [NSNumber numberWithFloat:value]; //number 本身不能算術運算，因此先轉成 float ，再變為 NSNumber
return result;
}

@end

int main () {

//    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //這與記憶體管理有關，但現在以是自動引用計數模式，已不可用了

SampleClass *sampleClass = [[SampleClass alloc] init];
NSNumber *a = [NSNumber numberWithFloat: 11.0];
NSNumber *b = [NSNumber numberWithFloat: 10.0];
NSNumber *result = [sampleClass addA:a withB:b];
NSString *resultString = [result stringValue]; //NSNumber 轉為 NSString
NSLog(@"結果為 %@",resultString);

return 0;
}
``````

``````2021-09-28 01:00:31.403729+0800 TestOC[15699:212863] 結果為 21
``````