Objective-c - NSNumber 및 NSValue

7082 단어 Objective-C
NSNumber
        //NSNumber       (boxing) ->       (unboxing) -> 
NSNumber *n1 = [NSNumber numberWithInt:100];
NSLog(@"n1: %@", n1);

NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:10];
[arr1 addObject:n1];
NSLog(@"arr1: %@", arr1);

NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithCapacity:10];
[dic1 setObject:n1 forKey:@"price"];
NSLog(@"dic1: %@", dic1);


//NSNumber
NSNumber *n2 = [NSNumber numberWithChar:'a'];
NSLog(@"n2: %@", n2);

NSNumber *n3 = [NSNumber numberWithBool:false];
NSLog(@"n3: %@", n3);

NSNumber *n4 = [NSNumber numberWithBool:true];
NSLog(@"n4: %@", n4);

NSNumber *n5 = [NSNumber numberWithDouble:100.08];
NSLog(@"n5: %@", n5);

//NSNumber
NSLog(@"n2: %c", [n2 charValue]);
NSLog(@"n5: %f", [n5 doubleValue]);

 
NSValue
        //NSValue   valueWithBytes:<#(const void *)#> objCType:<#(const char *)#>   valueWithRange 
// NSNumber , , (NSRect, NSRange, NSSize, NSPoint -> )

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

NSRect rect1 = NSMakeRect(3, -10, 30, 20);
NSValue *val1 = [NSValue valueWithRect:rect1];
[array addObject:val1];
NSLog(@"array: %@", array);

NSRange range1 = NSMakeRange(100, 2000);
NSValue *val2 = [NSValue valueWithRange:range1];
[array addObject:val2];
NSLog(@"array: %@", array);

NSSize size1 = NSMakeSize(10, 10);
NSValue *val3 = [NSValue valueWithSize:size1];
[array addObject:val3];
NSLog(@"array: %@", array);

NSPoint point1 = NSMakePoint(30, 50);
NSValue *val4 = [NSValue valueWithPoint:point1];
[array addObject:val4];
NSLog(@"array: %@", array);

NSRect rect2 = NSMakeRect(0, 0, 100, 300);
NSValue *val5 = [NSValue valueWithBytes:&rect2 objCType:@encode(NSRect)];
[array addObject:val5];
NSLog(@"array: %@", array);

NSPoint point2 = NSMakePoint(15, 90);
NSValue *val6 = [NSValue valueWithBytes:&point2 objCType:@encode(NSPoint)];
[array addObject:val6];
NSLog(@"array: %@", array);


//NSValue getValue( ) rectValue, pointValue, sizeValue
NSValue *v1 = [array objectAtIndex:0];
NSLog(@"v1: %@", v1);
NSRect rect3;
[v1 getValue:&rect3];
NSLog(@"location: %f, length: %f", rect3.origin.x, rect3.origin.y);

NSRect rect4 = [v1 rectValue];
NSLog(@"width: %f, height: %f", rect4.size.width, rect4.size.height);

좋은 웹페이지 즐겨찾기