NSValue.h 中,一般用的不是很多,
注:注意多使用 NSNumber,而且用 @(int); 方法装箱!
注:UIKit 中 IGeometry.h 中对系统的CG系列的结构体有拓展。使用比较方便。
NSValue
- 地址存各种 结构体
//结构体(可以自己创建)
CGRect rect = CGRectMake(111, 111, 22, 33);
//value 保存 rect 的地址(CGRect类型)
NSValue *rectValue = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
//新newRect
CGRect newRect;
//newRect就可以获取 rectValue 中的数据
[rectValue getValue:&newRect];
- 指针
NSValue* startValue = [NSValue valueWithPointer:@selector(start)];
// [startValue pointerValue]; // 就对应了 @selector(start);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(111, 111, 111, 111)];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[button addTarget:self action:[start pointerValue] forControlEvents:UIControlEventTouchUpInside];
- 弱引用
NSMutableArray *array = [NSMutableArray array];
// button 是一个 obj(假设是弱引用)
__weak UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
[button setFrame:CGRectMake(111, 111, 111, 111)];
// value obj
NSValue *value = [NSValue valueWithNonretainedObject:button];
[array addObject:value];
NSValue *newValue = [array objectAtIndex:0];
[self.view addSubview:(UIButton *)[newValue nonretainedObjectValue]];
- 获取类型
NSLog(@"%s",rectValue.objCType);
- 比较
NSValue *value1 = [NSValue valueWithCGSize:CGSizeMake(11, 11)];
NSValue *value2 = [NSValue valueWithRange:NSMakeRange(11, 11)];
if ([value1 isEqualToValue:value2]) {
NSLog(@"same");
}else{
NSLog(@"different");
}
NSNumber
- 实例方法
//实例方法 初始化
- (NSNumber *)initWithChar:(char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedChar:(unsigned char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithShort:(short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedShort:(unsigned short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInt:(int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInt:(unsigned int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLong:(long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLong:(unsigned long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLongLong:(long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithFloat:(float)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithDouble:(double)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithBool:(BOOL)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- 类方法
+ (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);
- 以上创建 基本不用
使用 @(someThing);// 快速装箱,自动判断类型。
NSNumber *number = @(1);
- 获取
@property (readonly) char charValue;
@property (readonly) unsigned char unsignedCharValue;
@property (readonly) short shortValue;
@property (readonly) unsigned short unsignedShortValue;
@property (readonly) int intValue;
@property (readonly) unsigned int unsignedIntValue;
@property (readonly) long longValue;
@property (readonly) unsigned long unsignedLongValue;
@property (readonly) long long longLongValue;
@property (readonly) unsigned long long unsignedLongLongValue;
@property (readonly) float floatValue;
@property (readonly) double doubleValue;
@property (readonly) BOOL boolValue;
@property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);
@property (readonly) NSUInteger unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);
// 这个灰常好用!
@property (readonly, copy) NSString *stringValue;
- 排序
NSNumber *number1 = [NSNumber numberWithInteger:13];
NSNumber *number2 = [NSNumber numberWithInteger:14];
NSComparisonResult result = [number1 compare:number2];
if (result == NSOrderedAscending) {
NSLog(@"升序");//number2 比 number1 大
}else if (result == NSOrderedSame){
NSLog(@"一致");
}else if (result == NSOrderedDescending){
NSLog(@"降序");
}else{
NSLog(@"error");
}
- 判断是否一致
NSNumber *number1 = [NSNumber numberWithInteger:13];
NSNumber *number2 = [NSNumber numberWithInteger:14];
if ([number1 isEqualToNumber:number2]) {
NSLog(@"same");
}else{
NSLog(@"nonono");
}
- 本地化的字符串,规范化。继续研究 NSLocale
NSNumber *number2 = [NSNumber numberWithInteger:1314];
NSString *string = [number2 descriptionWithLocale:[NSLocale currentLocale]];
NSLog(@"%@",string);
1