`
yuanlanjun
  • 浏览: 1186259 次
文章分类
社区版块
存档分类
最新评论

int,NSInteger,NSUInteger,NSNumber

 
阅读更多

1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。

2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。

3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。

NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:

NSArray *array = [[NSArray alloc]init];

[array addObject:3];//会编译错误

这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但'3'不是。这个时候需要用到NSNumber:

NSArray *array = [[NSArray alloc]init];

[array addObject:[NSNumber numberWithInt:3]];

Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。例如以下创建方法:

+ (NSNumber *) numberWithChar: (char) value;

+ (NSNumber *) numberWithInt: (int) value;

+ (NSNumber *) numberWithFloat: (float) value;

+ (NSNumber *) numberWithBool: (BOOL) value;

将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:

- (char) charValue;

- (int) intValue;

- (float) floatValue;

- (BOOL) boolValue;

- (NSString *) stringValue;

分享到:
评论

相关推荐

    AZFMDB:您可以快速方便地使用sqllite开发您的应用程序

    AZFMDB u can fast and convenient use sqllite to develop ...* 支持 bool, int, float, NSInteger, NSUInteger, CGFloat, NSTimeInterval, @"NSNumber",@"NSDictionary",@"NSMutableDictionary",@"NSArray",@"NSMuta

    01-加法计算器

    NSInteger result = num1Text.intValue + num2Text.intValue; // 改变文本框的值 self.resultLabel.text = [NSString stringWithFormat:@"%ld", (long)result]; // 退出键盘 // [self.num1TextField ...

    分页效果设置

    - (void)setSelectedIndex:(NSUInteger)selectedIndex; @end typedef NS_ENUM(NSInteger, MSegmentedControlIndicatorStyle){ MSegmentedControlIndicatorStyleDefault }; typedef NS_ENUM(NSInteger, ...

    IOS详细瀑布流

    static const NSInteger XMGDefaultColumnCount = 3; /** 每一列之间的间距 */ static const CGFloat XMGDefaultColumnMargin = 10; /** 每一行之间的间距 */ static const CGFloat XMGDefaultRowMargin = 10; /** ...

    详解 IOS下int long longlong的取值范围

    主要介绍了详解 IOS下int long longlong的取值范围的相关资料,需要的朋友可以参考下

    C++语言程序设计课后答案

    C++语言程序设计课后答案 清华大学 郑莉

    仿网易自动循环广告,也可手动滑动

    for int i 0; i < 3; ++i { UIImageView imageView [[UIImageView alloc]initWithFrame:self adViewContainerInPersonalView bounds]; imageView image [UIImage imageNamed:[NSString stringWithFormat:@&...

    使用核心数据最简单的方法

    特点:自定义 primaryKey,目前支持 NSString,NSInteger(int_64,int_32,int_16),NSNumber JSON(NSDictionary) -> NSManageObject(In theory,support any KVC object)JSONs(NSArray) -> NSManageObject(s)安全...

    LHDTableVIew简单封装

    @property (nonatomic, copy) NSInteger(^tableViewNumberOfRowInSection)(UITableView *,NSInteger); @property (nonatomic, copy) UITableViewCell *(^tableViewCellForRowAtIndexPath)(UITableView *, ...

    ios-MCAlart.zip

    [alart mcShowControllerAlartTitle:title ItmeNames:nil dataKey:nil ItmePlaceholders:pls showButtomAlert:buttom mcAlartReturnIndex:^(NSInteger mcItem, id data) { NSLog(@"收到值了---%ld-->%@",(long...

    不会说话的汤姆猫

    NSInteger frames=[dict[@"frames"] integerValue]; NSMutableArray * imageList=[NSMutableArray array]; for (NSInteger i=0; i; i++) { NSString *fileName=[NSString stringWithFormat:format,i]; UIImage*...

    ios-用一张ImageView实现无限轮播条(支持自动、点击).zip

    [banner setSelectImageBlock:^(NSInteger index){ NSLog(@"点击%d",index); }]; [self.view addSubview:banner]; //2秒后加载本地图片 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64...

    iOS基于ImageMagick的图片压缩<不支持模拟器>

    [[WZMQualityHandle shareHandle] compressImage:path quality:0.5 progress:^(NSString *outPath, CGFloat pro, NSInteger size0, NSInteger size1) { if (pro == 1.0) { NSLog(@"压缩完成,%@", outPath); } ...

    ios-DLCalender.zip

    _calendarView.calendarBlock = ^(NSInteger day, NSInteger month, NSInteger year){ NSString *dateString=[NSString stringWithFormat:@"%lu-%lu-%lu",year,month,day]; }; self.calendarView....

    Object-c字符串数组字典

    字符串、数组、字典、集 合以及快速OC语言中的字符串是NSString 初始化 -(id) initWithString:(NSString *)str; -(id) initWithFormat:(NSString *)format... ...-(unichar) characterAtIndex:(NSUInteger) index

    ios-点击图片查看大图.zip

    - (instancetype)initWithAllPhotosArray:(NSArray *)photosArr currentIndex:(NSInteger)currentIndex way:(NSInteger )way; 参数1:photosArr --- 保存URL的数组 @[@"URL1",@"URL2"] 参数2:currentIndex --- ...

    15个梨分给5个人的组合算法

    把15个梨分给5个人,每人最少2个,最多5个的组合算法,返回一个数组 - (NSArray *)createRandomArrayForCount:(NSInteger)aCount min:(NSInteger)aMin max:(NSInteger)aMax people:(NSInteger)aPeople

    ios-MCTopView.zip

    -(void)mcShowSelfViewTopView:(NSInteger)style{ MCTopView *topView = [MCTopView mcDefaultMCPickViewController:self setFrame:CGRectMake(0,100, self.view.bounds.size.width, 30)]; [topView ...

    iOS统计图表组件XXChartView.zip

    还可通过tintColor设置喜欢的颜色- (instancetype)initWithValues:(NSArray *)values xTittles:(NSArray *)xTittles yTittleCount:(NSInteger)yTittleCount; (instancetype)chartViewWithValues:(NSArray *)...

    ios-UITextField,UITextView限制输入长度和类型.zip

    typedef NS_ENUM(NSInteger, XLFTextLimitType) { XLFTextLimitTypeNone, // 无限制 XLFTextLimitTypeByte, // 字节数限制 XLFTextLimitTypeLength, // 字符个数限制 }; typedef NS_ENUM(NSInteger, ...

Global site tag (gtag.js) - Google Analytics