Object -C NSNumber -- 笔记
//
// main.m
// NSNumber
//
// Created by facial on 24/8/15.
// Copyright (c) 2015 facial_huo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSNumber *intNumber = [[NSNumber alloc] initWithInt: 1];
NSNumber *floatNumner = [[NSNumber alloc] initWithFloat:1.0f];
NSNumber *doubleNumber = [[NSNumber alloc] initWithDouble: 3.33];
NSNumber *charNumber = [[NSNumber alloc] initWithChar: 'A'];
//比较数字大小
NSNumber *intNumber2 = [[NSNumber alloc] initWithInt:2];
bool ret = [intNumber2 isEqualToNumber: intNumber]; // intNumber = 1, intNumber = 2. so ret = 0;
NSLog(@"%d",ret);
//比较float and int
bool ret2 = [intNumber isEqualToNumber: floatNumner];
// int intBumber = 1, floatNumber = 1.0f, so ret2 = 0;
//比较
NSNumber *num1 = [[NSNumber alloc] initWithInt: 10 ];
NSNumber *num2 = [[NSNumber alloc] initWithInt: 10 ];
if( [num1 compare:num2] == NSOrderedAscending ) {
NSLog(@" < ");
}else if([num1 compare:num2] == NSOrderedDescending) {
NSLog(@">");
}else if ([num1 compare:num2] == NSOrderedSame){
NSLog(@"==");
}
// 把NSNumber 类型转回去普通的类型
NSLog(@"%d", [intNumber intValue]);
NSLog(@"%f", [floatNumner floatValue]);
NSLog(@"%f", [doubleNumber doubleValue]);
NSLog(@"%c", [charNumber charValue]);
}
return 0;
}