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 SampleClass
10
11 - (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b
12 {
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 @end
21
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

Objective-C Numbers的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective C Runtime 开发介绍

    简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  7. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  9. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  10. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

随机推荐

  1. 机器学习之逻辑回归(logistic回归)

    前言            以下内容是个人学习之后的感悟,转载请注明出处~ 逻辑回归 一.为什么使用logistic回归   一般来说,回归不用在分类问题上,因为回归是连续型模型,而且受噪声影响比较大 ...

  2. Identity Server 4 原理和实战(完结)_建立Identity Server 4项目,Client Credentials 授权实例

    创建项目 dotnet new -i IdentityServer4.Templates 多出来的这些模板 adminUI用来测试,想要用再生产环境,需要交钱 结合core的 Identity来使用 ...

  3. AIRSDK 3.7 加载远程的含有代码的swf文件

    之前就说这个版本会解决可以加载远程的含有代码的swf文件的需求.但是,一直比较好奇这个是否行得通,还以为 Adobe 副总裁去了苹果,内部给了特殊待遇. 因为苹果一直就是不允许远程加载代码的,像js文 ...

  4. html5代码如何转成小程序代码

    插件源码 链接:https://pan.baidu.com/s/1pGY8ZsdESaQGEzoEgpb_Rw 提取码:s1ix  放到与pages文件夹同级目录下 js中调用 //引入 var wx ...

  5. 51nod - 1363 - 最小公倍数之和 - 数论

    https://www.51nod.com/Challenge/Problem.html#!#problemId=1363 求\(\sum\limits_{i=1}^{n}lcm(i,n)\) 先换成 ...

  6. ZOJ3228【AC自动机】

    先贡献几个数据(没用别怪我): /* ab 4 0 ab 1 ab 0 ab 1 ab abababac 4 0 aba 1 aba 0 abab 1 abab abcdefghijklmnopqrs ...

  7. performSegueWithIdentifier:sender里边的sender是啥意思

    performSegueWithIdentifier:sender里边的sender是啥意思啊?怎样用啊? [self performSegueWithIdentifier:@"pushSi ...

  8. Java EE规范下载

  9. 在Ubuntu14.04 64位上安装Clion

    1.下载Clion 1.1 下载Linux版Clion的.tar.gz的压缩包 Clion 2017.3.1 下载安装:https://www.jetbrains.com/clion/download ...

  10. python虚拟环境四

    python虚拟环境管理器 我们在使用python虚拟环境的时候,最好安装一个虚拟环境管理器,这样我们就能很方便的管理python的 虚拟环境,而python的虚拟环境管理工具包就是virtualen ...