Object-C 提供了NSValue 和NSNumber来封装C语言基本类型,这样就可以让他们具有面向对象的特征。
NSInteger : 大致等于 long 型整数
NSUInteger : 相当于 unsigned long
CGFloat : 在64位平台上相当于 double,32 为平台上相当于 float
NSValue 和NSNumber 都是包装类
1、NSValue 是NSNumber的基类,代表一个更通用的包装类,可以用于包装单个short,int,long,char,id,指针等数据类型
2、NSNumber 是更具体的包装类,主要用于包装C语言的各种数值类型,主要包含下列三个方法:
a . + numberWithXxx : 将特定类型的值包装成NSNumber。
b . -initWithXxx : 该实例方法需要创建一个NSNumber对象,再用一个基本类型的值
来初始化NSNumber。
c . -xxxValue : 该实例方法返回该NSNumber对象包装的基本类型的值
其类方法如下:
@interface NSNumber : NSValue
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
- (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;
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
- (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;
程序实例:
//
// main.m
// OC包装类
//
// Created by mac on 14-11-29.
// Copyright (c) 2014年 mac. All rights reserved.
//
// main.m
// OC包装类
//
// Created by mac on 14-11-29.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char *
argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
// 调用类方法将int类型的值包装成NSNumber对象
NSNumber* num = [[NSNumber alloc] initWithInt:28];
NSLog(@"%@" , [num class]);
// 调用类方法将double类型的值包装成NSNumber对象
NSNumber* de = [NSNumber numberWithDouble: 3.14];
NSLog(@"%d" , [num intValue]);
NSLog(@"%g" , [de doubleValue]);
// 先创建NSNumber对象,再调用initWithXxx方法执行初始化
NSNumber* ch = [[NSNumber alloc] initWithChar:'b'];
NSLog(@"%@",[ch class]);
// 直接输出NSNumber对象,使用%@格式字符串
NSLog(@"%@" , ch);
NSInteger m = 10 ;//相当于long 类型
NSLog(@"%li",m);
}
return 0;
}
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
// 调用类方法将int类型的值包装成NSNumber对象
NSNumber* num = [[NSNumber alloc] initWithInt:28];
NSLog(@"%@" , [num class]);
// 调用类方法将double类型的值包装成NSNumber对象
NSNumber* de = [NSNumber numberWithDouble: 3.14];
NSLog(@"%d" , [num intValue]);
NSLog(@"%g" , [de doubleValue]);
// 先创建NSNumber对象,再调用initWithXxx方法执行初始化
NSNumber* ch = [[NSNumber alloc] initWithChar:'b'];
NSLog(@"%@",[ch class]);
// 直接输出NSNumber对象,使用%@格式字符串
NSLog(@"%@" , ch);
NSInteger m = 10 ;//相当于long 类型
NSLog(@"%li",m);
}
return 0;
}
===>
2014-11-29 17:13:58.491 OC包装类[1430:303]
Hello, World!
2014-11-29 17:13:58.492 OC包装类[1430:303] __NSCFNumber
2014-11-29 17:13:58.493 OC包装类[1430:303] 28
2014-11-29 17:13:58.494 OC包装类[1430:303] 3.14
2014-11-29 17:13:58.495 OC包装类[1430:303] __NSCFNumber
2014-11-29 17:13:58.495 OC包装类[1430:303] 98
2014-11-29 17:13:58.496 OC包装类[1430:303] 10
2014-11-29 17:13:58.492 OC包装类[1430:303] __NSCFNumber
2014-11-29 17:13:58.493 OC包装类[1430:303] 28
2014-11-29 17:13:58.494 OC包装类[1430:303] 3.14
2014-11-29 17:13:58.495 OC包装类[1430:303] __NSCFNumber
2014-11-29 17:13:58.495 OC包装类[1430:303] 98
2014-11-29 17:13:58.496 OC包装类[1430:303] 10
Program ended with exit code: 0