NSNumber、NSValue、NSDate、NSObject

  注:OC中数组和字典只能存储OC对象不能存放基本数据类型。

NSNumber

  NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象。

NSNumber *number = [NSNumber numberWithInt:10]; // 将int类型的10 包装成一个NSNumber对象。
NSLog(@"number=%@", number);//NSNumber类型对象可以当作字符串直接输出。

NSMutableArray *array = [NSMutableArray array];
[array addObject:number]; // 添加数值到数组中

//取出来还是一个NSNumber对象,不支持自动解包(也就是不会自动转化为int类型):
NSNumber *number1 = [array lastObject];

// 将NSNumber转化成int类型:
int num = [number1 intValue];

常见初始化方法:
+(NSNumber *)numberWithChar:(char)value
+(NSNumber *)numberWithInt:(int)value
+(NSNumber *)numberWithFloat:(float)value
+(NSNumber *)numberWithBool:(BOOL)value

-(id)initWithChar:(char)value
-(id)initWithInt:(int)value
-(id)initWithFloat:(float)value
-(id)initWithBool:(BOOL)value

NSNumber常用方法:
-(char)charValue
-(int)intValue
-(double)doubleValue
-(BOOL)boolValue
-(NSString *)stringValue
-(NSComparisonResult)compare:(NSNumber *)otherNumber//比较两个数字对象的大小
-(BOOL)isEqualToNumber:(NSNumber *)number

 

NSValue

  NSNumber是NSValue的子类,但NSNumber只能包装数字类型,NSValue可以包装任意值,所以可以用NSValue包装结构体后加入NSArray、NSDictionary等集合中。

CGPoint point = CGPointMake(10, 10);

// 将结构体变量point包装成一个对象value:
NSValue *value = [NSValue valueWithPoint:point];

NSMutableArray *array = [NSMutableArray array];
[array addObject:value]; // 添加value进array

NSValue *value1 = [array lastObject]; // 取出当时放进去的value
CGPoint point1 = [value1 pointValue];//解包

BOOL result = CGPointEqualToPoint(point1, point);    // CGPointEqualToPoint()方法用来比较两个结构体是否相同。

typedef struct {
    int year;
    int month;
    int day;
} Date;//定义一个结构体
Date date = {2013, 4, 7};//给结构体赋值

//注:void * 代表任何指针
    
char *type = @encode(Date); //根据结构体类型生成 对应的 类型描述字符串
NSValue *value = [NSValue value:&date withObjCType:type]; // 这里要传结构体的地址&date。value参数是想要包装的数据的地址(如一个NSPoint的地址,可以用&来取地址),type参数是用来描述这个数据类型的字符串,用@encode()指令来生成。

Date date1; // 定义一个结构体变量
[value getValue:&date1]; // 取出包装好的结构体
[value objCType];  //取出类型描述字符串
NSLog(@"year=%i, month=%i, day=%i", date1.year, date1.month, date1.day);

//创建NSValue的常用方法:
-(id)initWithBytes:(const void *)value objCType:(const char *)type
+(NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type

//NSValue常用方法:
-(void)getValue:(void *)value //获取所包装的数据,保存到value这个地址。
-(const char *)objCType //返回描述所包装数据类型的字符串。
-(BOOL)isEqualToValue:(NSValue *)value
+(NSValue *)valueWithPoint:(NSPoint)point
+(NSValue *)valueWithSize:(NSSize)size
+(NSValue *)valueWithRect:(NSRect)rect
-(NSPoint)pointValue
-(NSSize)sizeValue
-(NSRect)rectValue

NSDate

NSDate *date = [NSDate date]; // date方法返回的就是当前时间(now)
date = [NSDate dateWithTimeIntervalSinceNow:10];//返回一个比当前时间晚10秒的一个时间。  
date = [NSDate dateWithTimeIntervalSince1970:10]; // 从1970-1-1 00:00:00开始返回一个推迟10秒的时间。
date = [NSDate distantFuture]; // 随机返回一个比较遥远的未来时间
date = [NSDate distantPast]; // 随机返回一个比较遥远的过去时间

//NSDate的静态初始化:
+(id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs //返回以当前时间为基准,然后过了secs秒的时间。
+(id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs
//返回以2001/01/01 GMT为基准,然后过了secs秒的时间。
+(id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs //返回以1970/01/01 GMT为基准,然后过了secs秒的时间。
+(id)distantFuture //返回很多年以后的未来的某一天。
+(id)distantPast //返回很多年以前的某一天.

//NSDate的动态初始化:
-(id)addTimeInterval:(NSTimeInterval)secs //返回以目前的实例中保存的时间为基准,然后过了secs秒的时间。
-(id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs
//初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。
-(id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate //初始化为以refDate为基准,然后过了secs秒的时间。
-(id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs //初始化为以当前时间为基准,然后过了secs秒的时间。

//NSDate取回时间间隔:
-(NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate //以refDate为基准时间,返回实例保存的时间与refDate的时间间隔。
-(NSTimeInterval)timeIntervalSinceNow //以当前时间(now)为基准时间,返回实例保存的时间与当前时间(now)的时间间隔。
-(NSTimeInterval)timeIntervalSince1970 //以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔。
-(NSTimeInterval)timeIntervalSinceReferenceDate //以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔。
+( NSTimeInterval)timeIntervalSinceReferenceDate //以2001/01/01 GMT为基准时间,返回当前时间(now)与2001/01/01 GMT的时间间隔。

//NSDate日期比较:
-(BOOL)isEqualToDate:(NSDate *)otherDate //与otherDate比较,相同返回YES。
-(NSDate *)earlierDate:(NSDate *)anotherDate //与anotherDate比较,返回较早的那个日期。
-(NSDate *)laterDate:(NSDate *)anotherDate //与anotherDate比较,返回较晚的那个日期。
-(NSComparisonResult)compare:(NSDate *)other
/*
该方法用于排序调用:
    当实例保存的日期值与other相同时返回NSOrderedSame
    当实例保存的日期值晚于other时返回NSOrderedDescending
    当实例保存的日期值早于other时返回NSOrderedAscending
**/

//NSDate日期格式化:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";    // HH是24进制,hh是12进制:
formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
NSString *string = [formatter stringFromDate:date];//日期转为字符串
NSLog(@"%@", string);

// 返回的格林治时间,字符串转为日期:
NSDate *date2 = [formatter dateFromString:@"2010-09-09 13:14:56"];

NSObject

Student *stu = [[[Student alloc] init] autorelease];

// isKindOfClass判断对象是否属于某个类 或者 子类:
if ( [stu isKindOfClass:[Person class]] ) {
     NSLog(@"stu属于Person或者继承自Person");
}

// isMemberOfClass判断对象是否属于某个类(不包括子类):
BOOL result = [stu isMemberOfClass:[Student class]];
NSLog(@"%i", result);

[stu performSelector:@selector(test)]; // 间接调用test方法。
[stu performSelector:@selector(test2:) withObject:@"abc"];//间接调用test2方法,@"abc"是test2:方法的参数。
[stu performSelector:@selector(test2:) withObject:@"abc" afterDelay:2]; // 延迟2秒后调用test2:方法。

//NSObject常用方法:
-(BOOL)conformsToProtocol:(Protocol)aProtocol    //判断对象是否实现了aProtocol协议。
+(BOOL)instancesRespondToSelector:(SEL)aSelector //判断这个类的对象是否拥有参数提供的方法。
-(BOOL)respondsToSelector:(SEL)aSelector //判断对象是否拥有参数提供的方法。

// 类的反射:
NSString *str = @"Person";
Class class = NSClassFromString(str);    //字符串变为类。
Person *person = [[class alloc] init];
NSLog(@"%@", person);
NSString *name =  NSStringFromClass([Person class]); // Class变成字符串

// 方法的反射:
NSString *method = @"test";
SEL selector = NSSelectorFromString(method);//字符串转换为方法类型。
[person performSelector:selector]; //调用方法选择器指定的方法selector.
NSString *selectorName = NSStringFromSelector(selector); // 将SEL转换为字符串
原文地址:https://www.cnblogs.com/CJDog/p/5127268.html