Centos系统下载大全 | Redhat系统下载大全 | Windows2012系统下载大全 | Windows2008系统下载大全| CMS教程 | 网站地图 51运维网-专注Linux/Unix系统安全运维!
当前位置:51运维网 > 编程 > Objective-C > 正文

Objective-C Numbers/数字

时间:2014-08-20 21:20 来源:未知 作者:www.51ou.com 阅读:

在Objective-C编程语言,为了保存的基本数据类型,如整型,浮点型,布尔对象的形式,

Objective-C中提供了一系列的NSNumber和重要的工作方法是下表中列出。

S.N. 方法和说明
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 www.yiibai.com
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 yiibai.com
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.

下面是一个简单的例子,使用的NSNumber两个数字相乘,并返回该产品。

#import <Foundation/Foundation.h>

@interface SampleClass:NSObject

- (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b;

@end

@implementation SampleClass

- (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b
{
   float number1 = [a floatValue];
   float number2 = [b floatValue];
   float product = number1 * number2;
   NSNumber *result = [NSNumber numberWithFloat:product];
   return result;
}

@end

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   SampleClass *sampleClass = [[SampleClass alloc]init];
   NSNumber *a = [NSNumber numberWithFloat:10.5];
   NSNumber *b = [NSNumber numberWithFloat:10.0];   
   NSNumber *result = [sampleClass multiplyA:a withB:b];
   NSString *resultString = [result stringValue];
   NSLog(@"The product is %@",resultString);

   [pool drain];
   return 0;
}  

现在,当我们编译并运行程序,我们会得到以下的结果。

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

感谢您对【51运维网 http://www.51ou.com/】的支持,我们为您免费提供《Objective-C Numbers/数字》技术文章,《Objective-C Numbers/数字》详细使用和说明,有时《Objective-C Numbers/数字》可能不完善、敬请谅解!如果《Objective-C Numbers/数字》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

顶一下
(0)
0%
踩一下
(0)
0%
上一篇:Objective-C 块
下一篇:Objective-C Arrays/数组
推荐内容
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码:点击我更换图片