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. +----+-----+ | ...
随机推荐
- bzoj 4668 冷战——并查集结构
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4668 不路径压缩,维护并查集的树的结构,查询链上最大值.按秩合并就可以暴爬. #includ ...
- 最短路——Dijkstra和Floyd
Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...
- java面试编程题
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问 ...
- 机器学习之逻辑回归(logistic回归)
前言 以下内容是个人学习之后的感悟,转载请注明出处~ 逻辑回归 一.为什么使用logistic回归 一般来说,回归不用在分类问题上,因为回归是连续型模型,而且受噪声影响比较大 ...
- TypeScript完全解读(26课时)_14.ES6和Nodejs中的模块
创建modules文件夹,我们的文件都写在这里面 modules下面新建index.js文件,在index.ts内引入这个js文件 es6的模块 最主要的两个关键字 import和export imp ...
- 极客时间_Vue开发实战_04.开发环境搭建
Vue CLI的形式搭建环境: vue create hello-world 我们选择default默认的配置,提供babel和eslint的支持.如果你已经对工程化的东西非常了解了.你可以选择自定义 ...
- 继承映射关系 joinedsubclass的查询
会出现下面这样的错一般是配置文件中的mapping和映射文件中的package路径或者class中的name路径不一致 org.hibernate.MappingException: Unknown ...
- CodeForces 628B New Skateboard 思维
B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Swift协议中类继承协议的mutating问题
之前实际开发的时候遇到的一个小问题,网上也没有找到相关说明.本来当时弄明白了想着记下来的,但是比较忙就耽搁了,趁今天休息记录一下. 首先,我们看一下下面这个两数之和的协议 protocol Test: ...
- Codeforces714C【映射】
题意: T次操作: +的话就是往 multiset 塞进一个: -的话就是往 multiset 去除一个: ?操作 思路: +和-操作就是处理字符串直接实现一个原字符串改成"01" ...