iOS中的NSNumber与NSValue

2023-11-22 09:02:37 浏览数 (1)

NSNumber(只能包装数字类型)

代码语言:javascript复制
    NSNumber *num1 = [NSNumber numberWithInt:10];
    NSNumber *num2 = [NSNumber numberWithFloat:10.1];
    NSNumber *num3 = [NSNumber numberWithBool:YES];
    NSNumber *num4 = @10;

NSValue(可以包装结构体)

代码语言:javascript复制
  (NSValue *)valueWithCGPoint:(CGPoint)point;
  (NSValue *)valueWithCGVector:(CGVector)vector;
  (NSValue *)valueWithCGSize:(CGSize)size;
  (NSValue *)valueWithCGRect:(CGRect)rect;
代码语言:javascript复制
    CGPoint point =  CGPointMake(10,20);
    NSValue *value = [NSValue valueWithCGPoint:point];
    NSLog(@"%@",value);//NSPoint: {10, 20}

0 人点赞