1. Foundation常用结构体使用
NSRange range = NSMakeRange(7,3); //非OC对象
//或者 NSRange range = CGRangeMake(7,3);
NSString str = NSStringFromRange(range);
NSLog(@“%@“,str);
类似的结构体还有:NSPoint,NSSize,NSRect
2. Foundation常用类的使用
(1)NSString
NSString *str = @“A String!”;//常量字符串,不需要管理内存
NSString *str1 = [[NSString alloc] init];//需要管理内存
str1 = @“A String!”;
[str1 release];
NSString *str2 = [[NSString alloc] initWithString: @“A String!”];
[str2 release];
str2 = [NSString stringWithString: @“A String!”];//通过类的静态方法创建的对象,一般不需要自己管理内存
NSString *str3 = [[NSString alloc] initWithUTF8String: @“A String!”];
[str3 release];
str3 = [NSString stringWithUTF8String: @“A String!”];
NSString *str4 = [[NSString alloc] initWithFormat: @“My name is %s”,”hellen”];
[str4 release];
str4 = [NSString stringWithFromat:@“My name is %s”,”hellen”];
//NSString 提供静态方法可以读取文本
NSError err;
NSString *str = [NSString stringWithContentsOfFile: @“/user/hellen/text.txt” encoding: NSUTF8StringEncoding error:&err];
if (err== nil){
NSLog(@“读取文件成功 :%@“,str);
}else {
NSLog(@“读取文件失败: %@“,err);
}
//读取远程文件
NSURL url = [NSURL URLWithString: @“http://www.baidu.com” ];
NSError *err;
NSString *str = [NSString stringWithContentsOfURL: url encoding:UTF8StringEncoding error:&err];
//字符串导出
NSString *str = @“123456”;
[str writeToFile:file atomically: YES encoding:UTF8StringEncoding error:&err];
(2) NSArray :不可改变的数组,通过静态方法创建的数组,不用做内存管理
//创建一个空数组
NSArray *array = [NSArray array];
//创建有一个元素的数组
array = [NSArray arrayWithObject:@“abc”];
//创建有多个元素的数组
array = [NSArray arrayWithObjects:@“a”,@“b”,@“c”,nil];
(3)
//测试数组
NSArray *array = [NSArrayarrayWithObjects:@"a",@"b",@"c",nil];
if ([arraycontainsObject:@"c"]) {
NSLog(@"包含");
}else{
NSLog(@"不包含");
}
//遍历数组
for (int i=0; i<array.count; i++) {
NSLog(@"%@",[arrayobjectAtIndex:i]);
}
for (id objin array) {
NSLog(@"%@",obj);
}
[array enumerateObjectsUsingBlock:
^(id obj,NSUInteger idx,BOOL *stop) {
NSLog(@"%zi-%@",idx,obj);
}];
NSEnumerator *enume = [arrayobjectEnumerator];
id obj =nil;
while (obj = [enumenextObject]) {
NSLog(@"%@",obj);
}
(4)数组排序#pragma mark 数组排序
void arraySort(){
NSArray *array = [NSArrayarrayWithObjects:@"3",@"2",@"1",@"4",nil];
NSArray *array2 = [arraysortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array2);
}
void arraySort2(){
Student *stu1 = [StudentstudentWithFirstName:@"Hongbo"lastName:@"Zhou"];
Student *stu2 = [StudentstudentWithFirstName:@"Mingkun"lastName:@"Huang"];
Student *stu3 = [StudentstudentWithFirstName:@"Dehua"lastName:@"Hong"];
Student *stu4 = [StudentstudentWithFirstName:@"Chunqin"lastName:@"Yang"];
NSArray *array = [NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nil];
//指定排序比较方法
NSArray *array2 = [arraysortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"%@",array2);
}
void arraySort3(){
Student *stu1 = [StudentstudentWithFirstName:@"Hongbo"lastName:@"Zhou"];
Student *stu2 = [StudentstudentWithFirstName:@"Mingkun"lastName:@"Huang"];
Student *stu3 = [StudentstudentWithFirstName:@"Dehua"lastName:@"Hong"];
Student *stu4 = [StudentstudentWithFirstName:@"Chunqin"lastName:@"Yang"];
NSArray *array = [NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nil];
NSArray *array2= [arraysortedArrayUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2) {
NSComparisonResult result = [obj1.lastnamecompare:obj2.lastname];
if (result ==NSOrderedSame) {
result = [obj1.firstnamecompare:obj2.firstname];
}
return result;
}];
NSLog(@"%@",array2);
}
void arraySort4() {
Student *stu1 = [StudentstudentWithFirstName:@"Hongbo"lastName:@"Zhou"];
Student *stu2 = [StudentstudentWithFirstName:@"Mingkun"lastName:@"Huang"];
Student *stu3 = [StudentstudentWithFirstName:@"Dehua"lastName:@"Hong"];
Student *stu4 = [StudentstudentWithFirstName:@"Chunqin"lastName:@"Yang"];
NSArray *array = [NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nil];
//排序描述器来排序
NSSortDescriptor *lastnameDesc = [NSSortDescriptorsortDescriptorWithKey:@"lastname"ascending:YES];
NSSortDescriptor *firstnameDesc = [NSSortDescriptorsortDescriptorWithKey:@"firstname"ascending:YES];
NSArray *desc = [NSArrayarrayWithObjects:lastnameDesc,firstnameDesc,nil];
NSArray *array2 = [arraysortedArrayUsingDescriptors:desc];
NSLog(@"%@",array2);
}
4.类的反射
//类的反射,字符串变成类
NSString *str = @"People";
Class class = NSClassFromString(str);
People *people = [[class alloc] init];
NSLog(@"%@",people);
//class变成字符串
NSString *name = NSStringFromClass([People class]);
NSLog(@"%@",name);
//方法的反射,通过字符串点用方法
NSString *method = @"test";
SEL selector = NSSelectorFromString(method);
[people performSelector:selector];
//方法名变成字符串
name = NSStringFromSelector(selector);
NSLog(@"%@",name);