一.基本类型,基于foundation数据类型,C语言数据类型等
*********************************************************************************************************
常见的object-c的数据类型有那些, 和C的基本数据类型有什么区别?如:NSInteger和int
object-c的数据类型有NSString,NSNumber,NSArray,NSMutableArray,NSData等等,这些都是class,创建后便是对象,而C语言的基本数据类型int,只是一定字节的内存空间,用于存放数值;NSInteger是基本数据类型,并不是NSNumber的子类,当然也不是NSObject的子类。NSInteger是基本数据类型Int或者Long的别名(NSInteger的定义typedef long NSInteger),它的区别在于,NSInteger会根据系统是32位还是64位来决定是本身是int还是Long。
********************************************************************************************************
对象类型常见的有
Objective-C也可以用C语言的构造类型,如数组(array)、结构体(struct)、同用体(union)等。
(对于基本类型变量,不需要用指针,也不用手动回收,方法执行结束会自动回收。)
二.对象类型常见的有:
NSlog NSString NSInteger NSURL NSImage NSNumber
1.NSLog格式如下
------------------------------------------格式化输出---------------------------------------------------
NSLog(@"02.格式化输出");
int integerType = 5;//整型
float floatType = 3.1415; //浮点型
double doubleType = 2.2033;//双浮点型
short int shortType = 200;//短整型
long long int longlongType = 7758123456767L;//长整型
char * cstring = "this is a string!";//c语言字符串
//整型
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);
---------------------------------------------------------------------------------------------------------
2.NSNumber:NSNumber是Objective-c的数字对象。需求考虑内存释放问题。
(1) NSNumber *number = [NSNumber numberWithInt:123];
(2) NSLog(@"%i",[number intValue]);
(3) NSLog(@"%i",[number retainCount]);
//输出
2010-12-29 16:02:35.040 HelloWorld[4710:a0f] 123
2010-12-29 16:02:35.042 HelloWorld[4710:a0f] 1
3.NSString和NSMutableString:NSString是不可变字符串(NSContantString),其变量和其本类型一样不需要手动释放(它的retainCount为-1)。
(1)NSString赋值:
NSString *str1 = @"str...."; //(不需要手动释放)
NSString *str2 = [[NSString alloc] initWithString:@"str..."]; //不需要手动释放
因为对NSString赋值,会产生成的对象,所在方法中用NSString作临时对象,也要考虑内存开消问题。
--------------------------------------- 写一个NSString类的实现-----------------------------------
+ (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
+ (id) stringWithCString: (const char*)nullTerminatedCString
encoding: (NSStringEncoding)encoding
{
NSString *obj;
obj = [self allocWithZone: NSDefaultMallocZone()];
obj = [obj initWithCString: nullTerminatedCString encoding: encoding];
return AUTORELEASE(obj);
}
-------------------------------------------------------------------------------------------------------
提问:对于语句NSString*obj = [[NSData alloc] init]; obj在编译时和运行时分别时什么类型的对象?
编译时是NSString的类型;运行时是NSData类型的对象
***********************************按字符串使用划分*********************************************************************
1.NSString(字符串):序列不可变,可以赋初值,可以赋值,不能添加。一旦被创建,便不能改变。
(1).NSString型数据创建的两种方式:
a. NSString *string1 = “字符串内容”;
b.NSString *string2 = [NSString stringWithFormat:“字符串内容 %i 内容。”,参数];
(即,A方法针对没有参数,B方法有参数)
(2)字符串的方法
a.字符串长度
? NSLog(“%ld”,[求长度的字符串名 length]); //求字符串长度
注意:空格,字母,汉字都为1个字符。“\”为转义和“/”区分,当出现多个“\”时注意,“\\”是输出一个“\”算作一个字符
长度。
b.NSString(字符串比较)方法一:- (BOOL)isEqualToString:(NSString *)string 注意: 布尔返回值,有两种可能。比较的不只是长度,还有内容。事例:NSSTring *str1=“内容1”;NSSTring *str2=“内容2”;NSLog( "大小比较:%ld",[str1 length]);
NSLog( "大小比较:%ld",[str2 length]);
if([str1 isEqualToString:str2]) {
NSLog(@"一样");
}
else {
NSLog(@"不一样");
}
方法二:NSLog( @"大小比较:%ld",string1 compare:string2 );
结果:输出1 前者小 输出-1 后者小 等于输出0
对字符串的小操作:
不区分大小写比较:
long ret2 = [ star1 caseInsensitiveCompare:str2];
结果是:0或1.
转大写:
NSString *ptr = [ string1 uppercaseString ];
转小写:
NSString *ptr = [ string1 lowercaseString ];
首字母大写,其余小写:
NSString *ptr = [ string1 capitalizedString ];
**********************************************************************************************************************
(2)NSMutableString是可变字符串,若用 “[[NSMutableString alloc] init...]”方法初始化,需要考虑手动释放。
1 NSString *str = @"this is str...";
2 NSMutableString *mstr = [NSMutableString stringWithString:str];
3 str = @"sss";
4 NSLog(@"%@",mstr);
5 NSLog(@"%@",str);
输出:
this is str...
sss /*注:因为NSMutableString是NSString的子类,实际应用中很可以把NSMutableString变量赋给NSString。所以若用NSString做类的属性,也会用手动释放的方式*/
1 //接口文件
2 @interface TestProperty : NSObject {
3 NSString *name;
4 NSInteger myInt;
5 }
6
7 @property (copy,nonatomic) NSString *name;
8 @property NSInteger myInt;
9
10 @end
1 //实现类
2 @implementation TestProperty
3 @synthesize name;
4 @synthesize myInt;
5
6 -(void) dealloc{
7 self.name = nil;
8 [super dealloc];
9 }
10
11 @end
例:
代码
1 int main (int argc, const char * argv[]) {
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
3
4 NSMutableString *str1 = [NSMutableString stringWithString:@"this is str"];
5 NSMutableString *str2 = [NSMutableString stringWithString:str1];
6 [str2 appendString:@"sss"];
7 NSLog(@"%@",str1);
8 NSLog(@"%@",str2);
9 [pool drain];
10 return 0;
11 }
12
13 //输出
14 2010-12-30 11:43:13.511 HelloWorld[2119:a0f] this is str
15 2010-12-30 11:43:13.521 HelloWorld[2119:a0f] this is strsss
16
/*可以看出str2不是指向str1的,而是新的对象!!*/
实例3:
(1).使用NSMutableString增加
NSMutableString *mutableString = [ NSMutableString stringWithCapacity:10 ];//定义一个长度为10的可变字符串空间。
//加内容
[ mutableString appendString:@"字符串"];//写入添加部分
NSRangr strRange = [ mutableString rangeOfString:@"字符串"];//查找内容
//使用NSMutableString删除
[ mutableString deleteCharacterInRange:strRange];
4.NSArray和NSMutableArray
(1)NSArray是不可变数组,一般用于保存固定数据。和NSString不同的是,NSArray有retainCount,所以释放问题。
NSMubleArray是变数组,可以直接对其值进行操作。也可考虑释放问题。
不可变数组
NSArray *str = @"data1:data2:data3";
(2)NSMubleArray是NSArray的子类。
可变数组
NSMutableArray *array = [NSMutableArray arrayWithCapacity:17];
1 NSArray *arr = [NSArray arrayWithObjects:@"Sep",@"Januay",@"",nil];
2 NSArray *arr_ = [arr sortedArrayUsingSelector:@selector(compare:)];
3 NSLog(@"%i",[arr retainCount]);
4 for(NSString *name in arr_){
5 NSLog(@"%@",name);
6 }
7
8 //输出
9 2010-12-29 13:36:16.830 HelloWorld[3325:a0f] 1
10 2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Januay
11 2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Sep
代码
1 NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"Sep",@"Januay",@"",nil];
2 [arr sortUsingSelector:@selector(compare:)];
3 NSLog(@"%i",[arr retainCount]);
4 for(NSString *name in arr){
5 NSLog(@"%@",name);
6 }
7
8 //输出
9 2010-12-29 13:41:34.925 HelloWorld[3415:a0f] 1
10 2010-12-29 13:41:34.928 HelloWorld[3415:a0f] Januay
11 2010-12-29 13:41:34.930 HelloWorld[3415:a0f] Sep
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*5. NSSet和NSMutableSet:NSSet和NSMutableSet分别是不可变集合和可变集合。集合是一组单值的操作。NSSet和NSMutableSet都需要考虑释放问题。(未学过)
代码
1 NSSet *set = [NSSet setWithObjects:[NSNumber numberWithInt:10],@"bb",@"aa",@"bb",@"aa",nil];
2 for(id *obj in set){
3 NSLog(@"%@",obj);
4 }
5 NSLog(@"%i",[set count]);
6 NSLog(@"%i",[set retainCount]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6. NSDictionary和NSMutableDictionary:dictionary是由键-对象对组成的数据集合。NSDictionay和NSMutableDicionary都需要考虑内存释放问题。
代码
1 NSDictionary *dict = [NSDictionary
2 dictionaryWithObjects:[NSArray arrayWithObjects:@"val1",@"val2",nil]
3 forKeys:[NSArray arrayWithObjects:@"key2",@"key1",nil]];
4
5 for(NSString *key in dict){
6 NSLog(@"%@",[dict objectForKey:key]);
7 }
8 NSLog(@"%i",[dict retainCount]);
9 [pool drain];
//输出
2010-12-29 15:37:42.745 HelloWorld[4085:a0f] val2
2010-12-29 15:37:42.748 HelloWorld[4085:a0f] val1
2010-12-29 15:37:42.749 HelloWorld[4085:a0f] 1
由上面结果可以看出Dicionary是按Key排序的。NSString
实例:
一个对象传值
NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"anfkahfkan" forKey:@"an"];
数组传值
NSArray *vauleArray=@[@"Alice",@"20",@"西安"];
NSArray *keyArray=@[@"name",@"age",@"location"];
7.NSEnumerator 枚举
枚举类型
NSArray *arr=@[@"父亲" ,@"母亲"];
NSEnumerator *en=[arr objectEnumerator];
id anObject;
while (anObject = [en nextObject])
{
NSLog(@"%@",anObject);
}
8.结构体(struct)
NSRang NSDtae NSNumber NSNul
NSPoint 几何图形点坐标CGPoint
NSSize 几何图形长度和宽度CGSize
NSRect 矩形数据类型CGRect
struct cGsise
{
float width;
float heght;
};
struct cGsise size={.width=20,.heght=30};
NSLog(@"宽为:%f高为:%f",size.width,size.heght);
struct date
{
int year;
int month;
int day;
};
struct date today={2015,05,19};
NSLog(@"%i-%i-%i",today.year,today.month,today.day);
typedef struct _NSRange
{
NSUInteger location;
NSUInteger length;
}NSRange;
NSString *st=@"abcdef";
NSRange nsr=NSMakeRange(4, 2);
NSRange nsr={4,2};
NSLog(@"%@",[st substringWithRange:nsr]);
CGSize size=NSMakeSize(100, 200);
NSLog(@"物体的宽度:%.2f",size.width);
NSLog(@"物体的高度:%.2f",size.height);
CGPoint point=NSMakePoint(3, 5);
NSLog(@"物体的横坐标:%.2f",point.x);
NSLog(@"物体的纵坐标:%.2f",point.y);
CGRect cgrect=NSMakeRect(0,0,0,0);
cgrect.origin=point;
cgrect.size=size;
NSLog(@"物体的宽度:%.2f",cgrect.size.width);
NSLog(@"物体的高度:%.2f",cgrect.size.height);
NSLog(@"物体的横坐标:%.2f",cgrect.origin.x);
NSLog(@"物体的纵坐标:%.2f",cgrect.origin.y);
NSDate *nsdate=[NSDate date];
NSLog(@"%@",nsdate);
NSNumber *number;
[NSNumber numberWithInt:10];
number=@'a';
number=@12345;
number=@12345ul;
number=@123456ll;
number=@3.15f;
number=@YES;
NSLog(@"%@",number);
[NSNull null];
字符串
NSString *str
字符串比较
-(BOOL) isEqualToString:(NSString *)aString;
-(NSComparisonesult)compare:(NSString *)string;//不区分大小写
9. id: 声明的对象具有运行时的特性,即可以指向任意类型的objcetive-c的对象;