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. FTP:文件传输协议(指令及响应代码)

    文件传输协议(FTP)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接使用类似 TELNET 协议在主机间交换命令 ...

  2. bzoj 4711 小奇挖矿 ——“承诺”类树形dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4711 对“承诺”有了更深的了解. 向外和向内要区分,所以 f [ i ][ j ] 表示根向 ...

  3. IOS造成卡顿的主要原因

    1. cellForRowAtIndexPath, 单元格视图重用, 注意尽量让所有视图重用, 只根据单元格row和section的不容更换不同的数据, 而不是每次都生成新的单元格, 这是程序奔溃的前 ...

  4. spark运行模式之二:Spark的Standalone模式安装部署

    Spark运行模式 Spark 有很多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则运行在集群中,目前能很好的运行在 Yarn和 Mesos 中,当然 Spark 还有自带的 Stan ...

  5. bzoj2806

    广义后缀自动机+二分+单调队列+dp 这道题其实就是一个简单dp,dp[i]表示匹配到i最长匹配多少,设val[i]表示当前位置和原串的最长公共长度,二分的长度是L,那么要求dp[i]=max(dp[ ...

  6. Flexpaper二次开发入门教程》(十) Flexpaper简单使用-第一个Flexpaper例子

    4. Flexpaper简单使用 通过上面三章的内容,大家对Flexpaper.SWFTools应该有大概的了解了,SWF文件也已经生成了,我们开始进入Flexpaper的使用的介绍. 本章中只演示F ...

  7. Outlook 开发2

    转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html C#使用如下代码调用Outlook2003发送邮 ...

  8. Oracle中CASE WHEN的用法实例

    实例演示: (1)查询表users中的数据. select u.id,u.realname,U.SEX from users u; 查询结果如下 ID    REALNAME SEX 1  10082 ...

  9. c/c++面试30-38之指针

    30 看代码写结果-----指针加减 #include <stdio.h> int main(void) { ] = { , , , , }; );//这里要特别注意,&a+1的值 ...

  10. Flutter实战视频-移动电商-09.首页_项目结构建立和获取数据

    09.首页_项目结构建立和获取数据 在config下创建service_url.dart 用来配置我们后端接口的配置文件 一个变量存 接口地址,一个接口方法地址 所有后天请求数据的方法都放在这个文件夹 ...