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. bzoj 2238 Mst——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2238 一条非树边可以对一条链的树边产生影响.注意是边,所以把边下放到点上,只要跳 top 时 ...

  2. This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended

    VS2005转换成VS2010时出现的问题: This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x05 ...

  3. c++ _宏与内联函数

    第一部分:宏为什么要使用宏呢?因为函数的调用必须要将程序执行的顺序转移到函数所存放在内存中的某个地址,将函数的程序内容执行完后,再返回到转去执行该函数前的地方.这种转移操作要求在转去执行前要保存现场并 ...

  4. UVa 1412 Fund Management (预处理+状压DP)

    题意:题意很难说清楚自己看原文,链接:UVa 1412 Fund Management 析:总体来说如果没有超时的话,这个题不是特别难,但是这个题很容易超时,主要是体现在状态转移时,很容易想到状态方程 ...

  5. 别用visual editor了,用WindowBuilder

    以前利用 Eclipse Visual Editor 项目构建 GUI,现在用WindowBuilder吧. 官网说: The Visual Editor project has been archi ...

  6. art-template在项目中的应用

    art-template 是一个简约.超快的模板引擎.它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器. 下面介绍在 ...

  7. gSoap学习笔记

    http://www.cnblogs.com/xiangism/archive/2012/11/14/2770242.html http://www.cnblogs.com/lvkun/archive ...

  8. Matplotlib 如何显示中文

    Python 3.x 主要是如下代码 import os font = FontProperties(fname=os.path.expandvars(r"%windir%\fonts\si ...

  9. 一篇文章彻底弄懂CAS实现SSO单点登录原理

    1. CAS 简介 1.1. What is CAS ? CAS ( Central Authentication Service ) 是 Yale 大学发起的一个企业级的.开源的项目,旨在为 Web ...

  10. E: 软件包 ffmpeg 没有可供安装的候选者

    问题:在DSO安装依赖项ffmpeg时遇到“E: 软件包 ffmpeg 没有可供安装的候选者”这一问题. 解决:在Ubuntu上gstreamer0.10-ffmpeg属于额外的版权受限程序,gstr ...