Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
2013-08-21
在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objective-C提供了一个 NSNumber
类来作为一个中转,可以将所有的普通数据类型转化为NSNumber类型,这样就符合Objective-C的消息机制了。
NSNumber
#import <Foundation/Foundation.h> int main (int argc, const char * argv[])
{ @autoreleasepool { //初始化NSNumber使用格式: numberWith+object
NSNumber * intNumber=[NSNumber numberWithInteger:100];
//跳到NSInter定义的地方可以看到:typedef long NSInteger; NSInteger 其实就是long类型,只不过是重新定义了一个名字罢了。
// 取intNumber的值使用格式: object+Value
NSInteger dwint=[intNumber integerValue];
NSLog(@"%ld",dwint);
}
return 0;
}
结果:
2013-08-21 11:42:07.151 NSNumber[492:707] 100
#import <Foundation/Foundation.h> int main (int argc, const char * argv[])
{ @autoreleasepool { // numberWith+object
NSNumber * intNumber=[NSNumber numberWithDouble:12345e+];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
} 结果:
2013-08-21 11:34:23.631 NSNumber[435:707] -9223372036854775808
#import <Foundation/Foundation.h> int main (int argc, const char * argv[])
{ @autoreleasepool { // numberWith+object
NSNumber * intNumber=[NSNumber numberWithDouble:12345e+];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
}
结果:
2013-08-21 11:36:19.330 NSNumber[451:707] 123450000000000
给已经初始化的NSNumber对象重新赋值,也就是改变他指向空间里面的内容,只能通过给他重新指向来改变它的值,不能改变它最开始指向空间里面的值,因为产生的是一个常量,
#import <Foundation/Foundation.h> int main (int argc, const char * argv[])
{ @autoreleasepool { NSNumber * intNumber=[NSNumber numberWithInteger:];
NSInteger dwint=[intNumber integerValue];
NSLog(@"%ld",dwint);
//NSLog(@"%d",[intNumber retainCount]); const
//[intNumber initWithInteger:100]; error ,cannot be sent to an abstract object of class
intNumber=[NSNumber numberWithInteger:];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
}
结果:
2013-08-21 13:43:39.382 NSNumber[556:707] 100
2013-08-21 13:43:39.389 NSNumber[556:707] 1000
附上NSNumber类的原型:
/* NSValue.h
Copyright (c) 1994-2011, Apple Inc. All rights reserved.
*/ #import <Foundation/NSObject.h> @class NSString, NSDictionary; @interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value;
- (const char *)objCType; @end @interface NSValue (NSValueCreation) - (id)initWithBytes:(const void *)value objCType:(const char *)type;
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
+ (NSValue *)value:(const void *)value withObjCType:(const char *)type; @end @interface NSValue (NSValueExtensionMethods) + (NSValue *)valueWithNonretainedObject:(id)anObject;
- (id)nonretainedObjectValue; + (NSValue *)valueWithPointer:(const void *)pointer;
- (void *)pointerValue; - (BOOL)isEqualToValue:(NSValue *)value; @end
//开始================================================================================
@interface NSNumber : NSValue - (char)charValue;
- (unsigned char)unsignedCharValue;
- (short)shortValue;
- (unsigned short)unsignedShortValue;
- (int)intValue;
- (unsigned int)unsignedIntValue;
- (long)longValue;
- (unsigned long)unsignedLongValue;
- (long long)longLongValue;
- (unsigned long long)unsignedLongLongValue;
- (float)floatValue;
- (double)doubleValue;
- (BOOL)boolValue;
- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);
- (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0); - (NSString *)stringValue; - (NSComparisonResult)compare:(NSNumber *)otherNumber; - (BOOL)isEqualToNumber:(NSNumber *)number; - (NSString *)descriptionWithLocale:(id)locale; @end @interface NSNumber (NSNumberCreation) - (id)initWithChar:(char)value;
- (id)initWithUnsignedChar:(unsigned char)value;
- (id)initWithShort:(short)value;
- (id)initWithUnsignedShort:(unsigned short)value;
- (id)initWithInt:(int)value;
- (id)initWithUnsignedInt:(unsigned int)value;
- (id)initWithLong:(long)value;
- (id)initWithUnsignedLong:(unsigned long)value;
- (id)initWithLongLong:(long long)value;
- (id)initWithUnsignedLongLong:(unsigned long long)value;
- (id)initWithFloat:(float)value;
- (id)initWithDouble:(double)value;
- (id)initWithBool:(BOOL)value;
- (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
- (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0); + (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0); @end
Objective-C ,ios,iphone开发基础:几个常用类-NSNumber的更多相关文章
- Objective-C ,ios,iphone开发基础:几个常用类-NSString
第一个字符串: //必须在字符串的前面加上@符号, NSString* str=@"shouqiang_Wei";//输出以%@输出. NSLog(@"%@", ...
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...
- Objective-C ,ios,iphone开发基础:http网络编程
- (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...
- Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
第一步:下载第三方库,点击 连接 下载, 第二部:准备数据库:按照连接&中博客的步骤实现数据库, 数据库的设计大致如下表: id username pas ...
随机推荐
- ocp 1Z0-042 1-60题解析
1. Because of a power outage,instance failure has occurred. From what point in the redo log does rec ...
- cannot load such file -- openssl
[test@localhost usr]$ /usr/local/ruby/bin/gem install bundler ERROR: Loading command: install (LoadE ...
- Running Solr with Maven
Solr is an open source search server which is built by using the indexing and search capabilities of ...
- MySQL Update语句用法
用一个表的某列值更新另外一个表的某列值的sql语句: update tableA a innner join tableB b on a.column_1 = b.column_1 set a.col ...
- Hadoop on Mac with IntelliJ IDEA - 1 解决input path does not exist问题
本文讲述使用IntelliJ IDEA时遇到Hadoop提示input path does not exist(输入路径不存在)的解决过程. 环境:Mac OS X 10.9.5, IntelliJ ...
- BW知识点总结及面试要点
1. 如何理解数据仓库? 数据仓库 是 一个面向主题的,集成的,相对稳定的,反应历史变化的数据集合,用于支持管理决策. 2. OLAP 和 OLTP的基本概念 和 区别? Ol ...
- Linux下进程的同步相互排斥实例——生产者消费者
linux下的同步和相互排斥 Linux sync_mutex 看的更舒服点的版本号= = https://github.com/Svtter/MyBlog/blob/master/Linux/pth ...
- delphi Sender和Tag的用法
var Form1: TForm1; SelectedColor:TColor;//clBlack; //Defaultimplementation{$R *.dfm}procedure TFor ...
- android Camera 数据流程分析
这篇文章主要针对其数据流程进行分析.Camera一般用于图像浏览.拍照和视频录制.这里先对图像浏览和拍照的数据流进行分析,后面再对视频电话部分进行分析. 1.针对HAL层对摄像头数据处理补充一下 Li ...
- 05.pathinfo的两种模式与模版和控制器之间的关系
<?php function dump($data){ echo '<pre>'; var_dump($data); echo '</pre>'; } dump($_SE ...