常用的结构体:
NSRange:
NSPoint:–(x,y)
NSString
创建字符串方法:
从文件读字符串
字符串写文件
其它方法(略,待补充)
NSMutableString
NSArray
给数组里的元素都发送相同消息:
数组的遍历
数组的排序:
派生出新的数组
其它
NSMutableArray
NSDictonary
遍历
NSMutableDictionary
NSSet
NSMutableSet
NSNumber
NSValue
系统自带结构体
自定义结构体
NSNull
NSDate
常用的结构体:
NSRange:
void range(){
//方法1
NSRange r1;
r1.location=9;
r1.length=3;
NSString *str1 = NSStringFromRange(r1);
NSLog(@"r1 is %@",str1);
//方法2
NSRange r2=NSMakeRange(9, 3);
NSString *str2 = NSStringFromRange(r2);
NSLog(@"r2 is %@",str2);
}
NSPoint:–(x,y)
void point(){
//方法1
NSPoint p1;
p1.x=8;
p1.y=9;
NSString *str1=NSStringFromPoint(p1);
NSLog(@"p1 is %@",str1);
//方法2
NSPoint p2 = NSMakePoint(8, 9);
NSString *str2=NSStringFromPoint(p2);
NSLog(@"p2 is %@",str2);
//方法3
NSPoint p3=CGPointMake(8, 9);
NSString *str3 = NSStringFromPoint(p3);
NSLog(@"p3 is %@",str3);
}
void range(){
//方法1
NSRange r1;
r1.location=9;
r1.length=3;
NSString *str1 = NSStringFromRange(r1);
NSLog(@"r1 is %@",str1);
//方法2
NSRange r2=NSMakeRange(9, 3);
NSString *str2 = NSStringFromRange(r2);
NSLog(@"r2 is %@",str2);
}
void point(){
//方法1
NSPoint p1;
p1.x=8;
p1.y=9;
NSString *str1=NSStringFromPoint(p1);
NSLog(@"p1 is %@",str1);
//方法2
NSPoint p2 = NSMakePoint(8, 9);
NSString *str2=NSStringFromPoint(p2);
NSLog(@"p2 is %@",str2);
//方法3
NSPoint p3=CGPointMake(8, 9);
NSString *str3 = NSStringFromPoint(p3);
NSLog(@"p3 is %@",str3);
}
NSSize::(width , height);
NSRect:包括NSPoint 和NSSize;
NSString
不可变字符串
创建字符串方法:
void stringCreate(){
//动态方法创建
//1
NSString *str1 = @"A string";
//2
NSString *str2 = [[NSString alloc] init];
str2 [email protected]"A string";
//3
NSString *str3 =[[NSString alloc] initWithUTF8String:"A string"];
//4
NSString *str4 =[[NSString alloc] initWithFormat:@"my age is %i",19];
//静态方法创建
//5
NSString *str5 =[NSString stringWithUTF8String:"A string"];
//6
NSString *str6 =[NSString stringWithFormat:@"my age is %i",19];
}
从文件读字符串
void stringCreate2(){
NSString *path[email protected]"/Users/llw/tmp/test.txt";
//CFStringConvertNSStringEncodingToEncoding(kCFStringEncodingGB_18030_2000);
NSError *error;
NSString *str1=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if(error){
NSLog(@"读取文件失败,%@",error);
}else{
NSLog(@"读取文件成功,content is %@",str1);
}
}
字符串写文件
void stringExport(){
NSString *str[email protected]"123456";
//如果文件不存在,则创建;
//如果文件夹不存在,则报错
NSString *path = @"/Users/llw/tmp/test.txt";
NSError *error;
//YES 代表要进行原子操作,也就是会先创建一个临时文件
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(error){
NSLog(@"写入文件失败,%@",[error localizedDescription]);
}else{
NSLog(@"写入文件成功,");
}
}
其它方法(略,待补充)
NSMutableString
可变字符串,是NSString的子类。类似于java中的stringBuffer
void operate(){
//初始化时分配10个字数的存储空间。
NSMutableString *str1 = [[NSMutableString alloc] initWithCapacity:10];
[str1 setString:@"1234"];//设置字符串内容
NSLog(@"str1 is %@",str1);
//拼接一个字符串
[str1 appendString:@"567"];
NSLog(@"str1 is %@",str1);
//拼接一个字符串
[str1 appendFormat:@"age is %i ",27];
//替换字符串 age-> no
NSRange range =[str1 rangeOfString:@"age"];
[str1 replaceCharactersInRange:;range withString:@"no"];
//插入字符串
[str1 insertString:@"abc" atIndex:2];
//删除字符串
range =[str1 rangeOfString:@"age"];
[str1 deleteCharactersInRange:range];
}
NSArray
列表,是不可变的。类似于java中的ArrayList
void arrayCreate(){
//1 创建一个空的数组
NSArray *array1 = [NSArray array];
//2 创建1个元素的数组
NSArray *array2 =[NSArray arrayWithObject:@"123"];
//3 创建有3个元素的数组,nil表示数组的结束
NSArray *array3 = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
unsigned long count=[array3 count];//等价于 array3.count
}
void arrayUse(){
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
if([array containsObject:@"a"]){
NSLog(@"contains a");
}
NSString *last =[array lastObject];
NSString *str1 = [array objectAtIndex:1];
int index = [array indexOfObject:@"c"];
}
给数组里的元素都发送相同消息:
void arrySetMessage(){
Student *stu1 = [[Student alloc]init];
Student *stu2 = [[Student alloc]init];
Student *stu3 = [[Student alloc]init];
NSArray *array =[[NSArray alloc] initWithObjects:stu1,stu2,stu3, nil];
//让数组中的所有对象都调用test方法,并传入参赛@“123”
//Student 类里面有test方法,可以传入一个参数
[array makeObjectsPerformSelector:@selector(test:) withObject:@"123"]
;
}
数组的遍历
void arrayFor(){
Student *stu1 = [[Student alloc]init];
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",stu1, nil];
//1
int count=array.count;
for (int i=0; i<count; i++) {
id obj = [array objectAtIndex:i];
NSLog(@"%i is %@",i,obj);
}
//2 快速遍历
for(id obj in array){
NSLog(@"%@",obj);
}
//3 利用block遍历,stop用于终止循环(break)
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi is %@",idx,obj);
//如果索引为1时停止遍历
if(idx ==1){
*stop =YES;
}
}];
//4利用迭代器遍历
//获取迭代器
NSEnumerator *enumerator = [array objectEnumerator];
//获取反序迭代器
//[array reverseObjectEnumerator];
id obj =nil;
while (obj =[enumerator nextObject]) {
NSLog(@"%@",obj);
};
NSArray *array1 = [enumerator allObjects];//返回迭代器中的所有没被遍历过的元素。
}
数组的排序:
void arraySort(){
NSArray *array = [NSArray arrayWithObjects:@"2",@"3",@"1",@"4", nil];
//1
//返回一个排好序的数组,原数组不变
//指定元素的比较方法compare:
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
//2 Student里定义排序方法
Student *stu1 =[Student studentWithFirstname:@"张" andLastname:@"三"] ;
Student *stu2 =[Student studentWithFirstname:@"李" andLastname:@"四"] ;
Student *stu3 =[Student studentWithFirstname:@"王" andLastname:@"五"] ;
Student *stu4 =[Student studentWithFirstname:@"赵" andLastname:@"六"] ;
NSArray *array3 = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
NSArray *array4 = [array3 sortedArrayUsingSelector:@selector(compareStudent:)];
//3 利用block排序
[array3 sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
NSComparisonResult result=[obj1.firstname compare:obj1.firstname];
if(result == NSOrderedSame){
result = [obj1.lastname compare:obj1.lastname];
}
return result;
}];
//4
NSSortDescriptor *fristnameAsc =[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
NSSortDescriptor *lastnameDesc =[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:NO];
NSArray *sort =[NSArray arrayWithObjects:fristnameAsc,lastnameDesc, nil];
NSArray *array5= [array3 sortedArrayUsingDescriptors:sort];
}
派生出新的数组
void arrayNew(){
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
NSArray *array2=[array arrayByAddingObject:@"d"];
NSArray *array3=[array arrayByAddingObjectsFromArray:[array arrayByAddingObject:@"d"]];
NSRange range =NSMakeRange(1, 2);
NSArray *array4=[array subarrayWithRange:range];
}
其它
void arrayOther(){
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
//用","连接数组中所有对象
NSString *str =[array componentsJoinedByString:@","];
//将一个数组持久化到文件中,文件内容是xml格式
NSString *path[email protected]"/Users/llw/tmp/test.xml";
[array writeToFile:path atomically:YES];
//从文件中读取。(文件内容有严格的格式要求)
NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
}
NSMutableArray
可变数组。
NSDictonary
字典,不可变的。
void dictCreate(){
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
//最常用方法
dict=[NSDictionary dictionaryWithObjectsAndKeys:
@"v1",@"k1",
@"v2",@"k2", nil];
NSArray *objects =[NSArray arrayWithObjects:@"v1",@"v2",@"v3", nil];
NSArray *keys =[NSArray arrayWithObjects:@"k1",@"k2",@"k3", nil];
dict =[NSDictionary dictionaryWithObjects:objects forKeys:keys];
}
void dictUse(){
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
@"v1",@"k1",
@"v2",@"k2", nil];
unsigned long count = dict.count;
//是不可变的,只能取值,不能修改值
id obj = [dict objectForKey:@"k2"];
//write to file
NSString *path[email protected]"/Users/llw/tmp/test.txt";
[dict writeToFile:path atomically:YES];
NSArray *keys = [dict allKeys];
NSArray *objects =[dict allValues];
//notFoundMarker 表示 根据key找不到值时用notFoundMarker代替
NSArray *objects2 = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2", nil] notFoundMarker:@"no found"];
}
遍历
void dictFor(){
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
@"v1",@"k1",
@"v2",@"k2", nil];
//1
for (id key in dict) {
id value = [dict objectForKey:key];
}
//2 使用迭代器
//分key迭代器 ,value迭代器
NSEnumerator *enumer =[dict keyEnumerator];
id key =nil;
while (key = [enumer nextObject]) {
id value = [dict objectForKey:key];
}
NSEnumerator *enumer1 =[dict objectEnumerator];
//3 block,stop用于终止循环(break)
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"[%@ %@]",key,obj);
}];
}
NSMutableDictionary
可变字典
NSSet
NSMutableSet
NSNumber
NSNumber
可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray,NSDictionary等集合中。
void number(){
NSNumber *number = [NSNumber numberWithInt:10];
NSMutableArray *array =[NSMutableArray array];
[array addObject:number];
//取出来还是number,不会自动解包
NSNumber *number1 =[array lastObject];
int num= [number1 intValue];
}
NSValue
NSNumber是NSValue的子类,但NSNumber只能包装基本数据类型,NSValue可以包装任意值,也就可以用NSValue包装结构体后加入NSArray,等集合中。
系统自带结构体
void value(){
CGPoint point =CGPointMake(10, 10);
//将结构体包装成一个对象
NSValue *value = [NSValue valueWithPoint:point];
NSMutableArray *array =[NSMutableArray array];
[array addObject:value];
NSValue *value1 =[array lastObject];
CGPoint point1 =[value1 pointValue];
BOOL result = CGPointEqualToPoint(point,point1);
}
自定义结构体
typedef struct{
int year;
int month;
int day;
} Date;
void value2(){
Date date= {2013,4,7};
//void * 代表任何指针
//这里要传结构体的地址
//根据结构体类型生成 对应的 类型描述字符串
char *type =@encode(Date);
NSValue * value = [NSValue value:&date withObjCType:type];
//定义一个结构体变量
Date date1;
//取出包装好的结构体
[value getValue:&date1 ];
}
NSNull
集合中不能存放nil值,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么久可以使用NSNull,它也是NSObject的一个子类。
//[NSNull null]返回的是同一个单例对象,所以n = n1
NSNull *n =[NsNull null];
NSNull *n1 =[NSNull null];
NSDate
时间: 2024-10-22 04:24:45