Objective-C Numbers,objective-cnumbers

來源:互聯網
上載者:User

Objective-C Numbers,objective-cnumbers

In Objective-C programming language, in order to save the basic data types like int, float, bool in object form,

Objective-C provides a range of methods to work with NSNumber and important ones are listed in following table.

 

S.N. Method and Description
1 + (NSNumber *)numberWithBool:(BOOL)value

Creates and returns an NSNumber object containing a given value, treating it as a BOOL.

2 + (NSNumber *)numberWithChar:(char)value

Creates and returns an NSNumber object containing a given value, treating it as a signed char.

3 + (NSNumber *)numberWithDouble:(double)value

Creates and returns an NSNumber object containing a given value, treating it as a double.

4 + (NSNumber *)numberWithFloat:(float)value

Creates and returns an NSNumber object containing a given value, treating it as a float.

5 + (NSNumber *)numberWithInt:(int)value

Creates and returns an NSNumber object containing a given value, treating it as a signed int.

6 + (NSNumber *)numberWithInteger:(NSInteger)value

Creates and returns an NSNumber object containing a given value, treating it as an NSInteger.

7 - (BOOL)boolValue

Returns the receiver's value as a BOOL.

8 - (char)charValue

Returns the receiver's value as a char.

9 - (double)doubleValue

Returns the receiver's value as a double. 

10 - (float)floatValue

Returns the receiver's value as a float.

11 - (NSInteger)integerValue

Returns the receiver's value as an NSInteger.

12 - (int)intValue

Returns the receiver's value as an int.

13 - (NSString *)stringValue

Returns the receiver's value as a human-readable string.

Here is a simple example for using NSNumber which multiplies two numbers and returns the product.

 1 #import <Foundation/Foundation.h> 2  3 @interface SampleClass:NSObject 4  5 - (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b; 6  7 @end 8  9 @implementation SampleClass10 11 - (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b12 {13    float number1 = [a floatValue];14    float number2 = [b floatValue];15    float product = number1 * number2;16    NSNumber *result = [NSNumber numberWithFloat:product];17    return result;18 }19 20 @end21 22 int main()23 {24    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];25 26    SampleClass *sampleClass = [[SampleClass alloc]init];27    NSNumber *a = [NSNumber numberWithFloat:10.5];28    NSNumber *b = [NSNumber numberWithFloat:10.0];   29    NSNumber *result = [sampleClass multiplyA:a withB:b];30    NSString *resultString = [result stringValue];31    NSLog(@"The product is %@",resultString);32 33    [pool drain];34    return 0;35 }

Now when we compile and run the program, we will get the following result.

 1 2013-09-14 18:53:40.575 demo[16787] The product is 105 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.