Objective-c语法之基本数据类型转换&使用

本文介绍了Objective-C中的基本数据类型,包括int、float、double等,并详细讲解了如何使用NSLog进行格式化输出。此外,还探讨了NSInteger、NSUInteger、NSNumber的区别及使用场景,以及NSString与NSInteger之间的相互转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新创建项目是名称介绍:

Product Name: 指产品名称 ,类似于项目名称。
Company Identifier: 公司标识符,一般命名规则为 “com.公司名”
Bundle Identifier: 指包标识符,用于唯一标识应用程序,默认会根据公司标识符和产品名来组合生成
Device Family: 指该应用支持的设备类型,共三个选项:iPhone、iPad、Universal(即iPhone、iPad通用)

oc的基本数据类型有int float double char 四种基本数据类型;

1、基本数据类型格式输出数据

    //整型
    int integerType = 5;
    //浮点型
    float floatType = 3.1415;
    //双浮点型
    double doubleType = 2.2033;
    //短整型
    short int shortType = 200;
    //长整型
    long long int longlongType = 7758123456767L;
    //c语言字符串
    char * cstring = "this is a string!";
    
    
    //整型
    NSLog(@"The value of integerType = %d",integerType);
    //浮点型
    NSLog(@"The value of floatType = %.2f",floatType);
    //双浮点型
    NSLog(@"The value of doubleType = %e",doubleType);
    //短整型
    NSLog(@"The value of shortType = %hi",shortType);
    //长整型
    NSLog(@"The value of longlongType = %lli",longlongType);
    //c语言字符串
    NSLog(@"The value of cstring = %s",cstring);

打印结果

2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!

2、int,NSInteger,NSUInteger,NSNumber

1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
 NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:

NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObject:[NSNumber numberWithInt:88]];

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;

 NSNumber *num = [NSNumber numberWithInt:88];
    NSInteger integer = [num intValue];

3、NSString与NSInteger的相互转换

// NSInteger 转 NSString

    NSInteger integerNumber = 888;
    NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
    NSLog(@"string is %@", string);    
// NSString 转NSInteger

    integer = [string intValue];
    NSLog(@"integer is%d", integerNumber);
- (char) charValue;
- (int) intValue;
- (float) floatValue;
- (BOOL) boolValue;
- (NSString *) stringValue;
char float 等类型一样可以转换;


4、使用property属性的定义

@property (nonatomic, strong) NSArray arr;

一般  copy 用于NSString 

strong 用于具有可变数据,比如数组、字典、date 、对象

assign 用于基本数据类型,比如int long char bool 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值