OC语言的基本数据类型长度
NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool)); // Do any additional setup after loading the view,
运行结果:
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.
2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.
2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.
2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.
格式化输出数据
//整型
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!
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]];
这样是会引发编译错误的,因为NSMutableArray里面放的需要是一个类,但‘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];
NSString与NSInteger的相互转换
NSInteger integerNumber = 888;
NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
NSLog(@"string is %@", string);
integer = [string intValue];
NSLog(@"integer is%d", integerNumber);
OC是增强了C的特性,所以在变量和基本数据类型上基本与C一致。
在OC中变量命名有如下规则:
由字母、数字、下划线、$符号组成
必须以字母、下划线、$符号开头
大小写敏感
在OC中定义变量的时候不能使用OC的保留字,OC的保留字如下:
OC中有如下基本数据类型:
int:声明整型变量
double:声明双精度变量
float:声明浮点型变量
char:声明字符型变量
id:通用的指针类型
enum:声明枚举类型
long:声明长整型变量或函数
short:声明短整型变量或函数
signed:声明有符号类型变量
struct:声明结构体变量
union:声明共用体(联合)数据类型
unsigned:声明无符号类型变量
void:声明函数无返回值或无参
数据类型格式化字符:
数据类型 oc关键字 格式说明引导符
整型 int %d.%i
短整型 short int %hd.%hi
长类型 long int %ld.%li
无符号短整型 unsigned int %u
无短整型 unsigned short %hu
无符号长整型 unsigned long %lu
浮点型 float %f
双精度型 double %f
长双精度型 long double %lf
字符型 char %c
下面有一个例子程序:
//
// main.m
// mxy01-dataType
//
// Created by mxy on 13-9-7.
// Copyright (c) 2013年 mxy. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//保留字是不能定义为变量名的,例如下面的int、float等就不能作为变量名
int i = 2;
float f = 2.3f;
double d = 2.3e12;
char c = 'a';
//输出数据
NSLog(@"i : %d",i);
NSLog(@"f : %f 截取后 : %.2f",f,f);
NSLog(@"d : %e 截取后 : %.2e",d,d);
NSLog(@"c : %c , %d",c ,c );
//数据转换
//数据类型容量大的转成小的可能会丢失精度
int i2 = (int)f;
float f2 = (float)i ;
NSLog(@"数据转换");
NSLog(@"i2 : %d",i2);
NSLog(@"f2 : %f",f2);
NSLog(@"变量的作用域");
if(YES){
int i3 = 2;
NSLog(@"i3 : %d",i3);
}
//在if的{}外面是无法访问到内部的i3变量的,因为i3变量的作用域就只是那对{}内部
//NSLog(@"i3 : %d",i3);
/*
运行结果
2013-09-07 22:47:52.655 mxy01-dataType[859:303] i : 2
2013-09-07 22:47:52.667 mxy01-dataType[859:303] f : 2.300000 截取后 : 2.30
2013-09-07 22:47:52.672 mxy01-dataType[859:303] d : 2.300000e+12 截取后 : 2.30e+12
2013-09-07 22:47:52.674 mxy01-dataType[859:303] c : a , 97
2013-09-07 22:47:52.679 mxy01-dataType[859:303] 数据转换
2013-09-07 22:47:52.682 mxy01-dataType[859:303] i2 : 2
2013-09-07 22:47:52.685 mxy01-dataType[859:303] f2 : 2.000000
*/
}
return 0;
}