Objective-C Numbers
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的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective C Runtime 开发介绍
简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [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 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
随机推荐
- nvidia-smi 查看GPU信息字段解读
第一栏的Fan:N/A是风扇转速,从0到100%之间变动,这个速度是计算机期望的风扇转速,实际情况下如果风扇堵转,可能打不到显示的转速.有的设备不会返回转速,因为它不依赖风扇冷却而是通过其他外设保持低 ...
- 双重检查锁实现单例(java)
单例类在Java开发者中非常常用,但是它给初级开发者们造成了很多挑战.他们所面对的其中一个关键挑战是,怎样确保单例类的行为是单例?也就是说,无论任何原因,如何防止单例类有多个实例.在整个应用生命周期中 ...
- 【转载】“error LNK1169: 找到一个或多个多重定义的符号”的解决方法
c++的小细节的地方 原文地址:https://blog.csdn.net/xiaosu123/article/details/5665729 问题描述如下: 有 三个源文件,A.h.B.cpp.C. ...
- TypeScript完全解读(26课时)_17.装饰器
实验性的特性,需要在tslint里面把这项设置为true 作用域类的声明方法.访问符.属性和参数上 使用@符号加一个名字来定义,名字必须是一个函数,或者求值后是一个函数 装饰器工厂,setPro当做一 ...
- 一个IP绑定多个域名的实现方法
方案一: 文字叙述: 具体步骤如下:比如讲apache服务器127.0.0.1 配置成 www.sohu.com 首先在http.conf文件中 做如下处理: ①关闭默认的 #DocumentRoot ...
- POJ 3658 Artificial Lake (单调栈)
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...
- fitnesse(gradle构建)安装步骤
1.安装jdk.ant.gradle(参考http://www.cnblogs.com/274914765qq/p/4401525.html) 2.下载Fitnesse https://github. ...
- 使用Spring Security控制会话
1.概述 在本文中,我们将说明Spring Security如何允许我们控制HTTP会话.此控件的范围从会话超时到启用并发会话和其他高级安全配置. 2.会话何时创建? 我们可以准确控制会话何时创建以及 ...
- 动态加载dll
extern "C" MMUPDATENOTIFY_IMPEXP bool _cdecl NotifyThrift(char* chThriftIp, char* chPor) H ...
- ZOJ3175【公式化函数的思想】
题意: 给出f(n,m)(m<n)的定义:大于m并且小于n的能整除m的数的个数. F(n)为m从1至n的f(n,m)的和. 给出n,求F(n). 思路: 就是计算n/1 + n/2 + n/ ...