程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> [Objective-]OC中NSNumber數字對象的常用處理方法

[Objective-]OC中NSNumber數字對象的常用處理方法

編輯:關於C語言

之前已經學習過了基本數據類型。但是這些都市基本數據類型,它們不是對象。因此,不能向它們發送消息。

然後有時也需要將它們當做對象來處理。不如,我們需要把基本數據類型放入到集合中(在Cocoa中集中只能存放對象,

無法存放基本數據類型),那麼這時我們需要將基本類型轉換成數字對象.OC中提供了數據對象”NSNumber“可以將基本數據

類型”包裝“成對象,這樣我們就可以將基本數據類型來處理了

下面我們來看下具體使用例子:

 

//
//  main.m
//  FoundationDemo1
//
//  Created by hmjiangqq on 14-1-23.
//  Copyright (c) 2014年 hmjiangqq. All rights reserved.
//

#import 

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        /*===========NSNumber=================*/
         //創建一個NSNumber對象
        NSNumber *monthNumber=[[NSNumber alloc]initWithInt:10];
        NSNumber *lengthNumber=[[NSNumber alloc]initWithFloat:10.8];
        NSLog(@"month =%@\n",monthNumber);
        NSLog(@"length =%@\n",lengthNumber);
        //還原成基本數據類型
        int month=[monthNumber intValue];
        float length=[lengthNumber floatValue];
        NSLog(@"month=%d\n",month);
        NSLog(@"length=%.1f\n",length);
        /*=============NSString====================*/
    }
    return 0;
}
上面只是使用到了NSNumber中的兩個包裝和還原的方法,在NSNumber中還有很多相類似的方法:

 

 

- (id)initWithChar:(char)value;
- (id)initWithUnsignedChar:(unsigned char)value;
- (id)initWithShort:(short)value;
- (id)initWithUnsignedShort:(unsigned short)value;
- (id)initWithInt:(int)value;
- (id)initWithUnsignedInt:(unsigned int)value;
- (id)initWithLong:(long)value;
- (id)initWithUnsignedLong:(unsigned long)value;
- (id)initWithLongLong:(long long)value;
- (id)initWithUnsignedLongLong:(unsigned long long)value;
- (id)initWithFloat:(float)value;
- (id)initWithDouble:(double)value;
- (id)initWithBool:(BOOL)value;
- (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
- (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
與之相反的方法如下:

 

 

- (char)charValue;
- (unsigned char)unsignedCharValue;
- (short)shortValue;
- (unsigned short)unsignedShortValue;
- (int)intValue;
- (unsigned int)unsignedIntValue;
- (long)longValue;
- (unsigned long)unsignedLongValue;
- (long long)longLongValue;
- (unsigned long long)unsignedLongLongValue;
- (float)floatValue;
- (double)doubleValue;
- (BOOL)boolValue;
- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);
- (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);
當然我們還可以使用NSNumber的靜態方法進行創建對象,方法如下:

 

 

+ (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);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved