码迷,mamicode.com
首页 > 其他好文 > 详细

3、Foundation

时间:2016-02-21 11:24:00      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

常用的结构体:


NSRange:

  1. void range(){
  2. //方法1
  3. NSRange r1;
  4. r1.location=9;
  5. r1.length=3;
  6. NSString *str1 = NSStringFromRange(r1);
  7. NSLog(@"r1 is %@",str1);
  8. //方法2
  9. NSRange r2=NSMakeRange(9, 3);
  10. NSString *str2 = NSStringFromRange(r2);
  11. NSLog(@"r2 is %@",str2);
  12. }

NSPoint:–(x,y)

  1. void point(){
  2. //方法1
  3. NSPoint p1;
  4. p1.x=8;
  5. p1.y=9;
  6. NSString *str1=NSStringFromPoint(p1);
  7. NSLog(@"p1 is %@",str1);
  8. //方法2
  9. NSPoint p2 = NSMakePoint(8, 9);
  10. NSString *str2=NSStringFromPoint(p2);
  11. NSLog(@"p2 is %@",str2);
  12. //方法3
  13. NSPoint p3=CGPointMake(8, 9);
  14. NSString *str3 = NSStringFromPoint(p3);
  15. NSLog(@"p3 is %@",str3);
  16. }

NSSize::(width , height);
NSRect:包括NSPoint 和NSSize;

NSString

不可变字符串

创建字符串方法:

  1. void stringCreate(){
  2. //动态方法创建
  3. //1
  4. NSString *str1 = @"A string";
  5. //2
  6. NSString *str2 = [[NSString alloc] init];
  7. str2 =@"A string";
  8. //3
  9. NSString *str3 =[[NSString alloc] initWithUTF8String:"A string"];
  10. //4
  11. NSString *str4 =[[NSString alloc] initWithFormat:@"my age is %i",19];
  12. //静态方法创建
  13. //5
  14. NSString *str5 =[NSString stringWithUTF8String:"A string"];
  15. //6
  16. NSString *str6 =[NSString stringWithFormat:@"my age is %i",19];
  17. }

从文件读字符串

  1. void stringCreate2(){
  2. NSString *path=@"/Users/llw/tmp/test.txt";
  3. //CFStringConvertNSStringEncodingToEncoding(kCFStringEncodingGB_18030_2000);
  4. NSError *error;
  5. NSString *str1=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
  6. if(error){
  7. NSLog(@"读取文件失败,%@",error);
  8. }else{
  9. NSLog(@"读取文件成功,content is %@",str1);
  10. }
  11. }

字符串写文件

  1. void stringExport(){
  2. NSString *str=@"123456";
  3. //如果文件不存在,则创建;
  4. //如果文件夹不存在,则报错
  5. NSString *path = @"/Users/llw/tmp/test.txt";
  6. NSError *error;
  7. //YES 代表要进行原子操作,也就是会先创建一个临时文件
  8. [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
  9. if(error){
  10. NSLog(@"写入文件失败,%@",[error localizedDescription]);
  11. }else{
  12. NSLog(@"写入文件成功,");
  13. }
  14. }

其它方法(略,待补充)

NSMutableString

可变字符串,是NSString的子类。类似于java中的stringBuffer

  1. void operate(){
  2. //初始化时分配10个字数的存储空间。
  3. NSMutableString *str1 = [[NSMutableString alloc] initWithCapacity:10];
  4. [str1 setString:@"1234"];//设置字符串内容
  5. NSLog(@"str1 is %@",str1);
  6. //拼接一个字符串
  7. [str1 appendString:@"567"];
  8. NSLog(@"str1 is %@",str1);
  9. //拼接一个字符串
  10. [str1 appendFormat:@"age is %i ",27];
  11. //替换字符串 age-> no
  12. NSRange range =[str1 rangeOfString:@"age"];
  13. [str1 replaceCharactersInRange:;range withString:@"no"];
  14. //插入字符串
  15. [str1 insertString:@"abc" atIndex:2];
  16. //删除字符串
  17. range =[str1 rangeOfString:@"age"];
  18. [str1 deleteCharactersInRange:range];
  19. }

NSArray

列表,是不可变的。类似于java中的ArrayList

  1. void arrayCreate(){
  2. //1 创建一个空的数组
  3. NSArray *array1 = [NSArray array];
  4. //2 创建1个元素的数组
  5. NSArray *array2 =[NSArray arrayWithObject:@"123"];
  6. //3 创建有3个元素的数组,nil表示数组的结束
  7. NSArray *array3 = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
  8. unsigned long count=[array3 count];//等价于 array3.count
  9. }
  10. void arrayUse(){
  11. NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
  12. if([array containsObject:@"a"]){
  13. NSLog(@"contains a");
  14. }
  15. NSString *last =[array lastObject];
  16. NSString *str1 = [array objectAtIndex:1];
  17. int index = [array indexOfObject:@"c"];
  18. }

给数组里的元素都发送相同消息:

  1. void arrySetMessage(){
  2. Student *stu1 = [[Student alloc]init];
  3. Student *stu2 = [[Student alloc]init];
  4. Student *stu3 = [[Student alloc]init];
  5. NSArray *array =[[NSArray alloc] initWithObjects:stu1,stu2,stu3, nil];
  6. //让数组中的所有对象都调用test方法,并传入参赛@“123”
  7. //Student 类里面有test方法,可以传入一个参数
  8. [array makeObjectsPerformSelector:@selector(test:) withObject:@"123"]
  9. ;
  10. }

数组的遍历

  1. void arrayFor(){
  2. Student *stu1 = [[Student alloc]init];
  3. NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",stu1, nil];
  4. //1
  5. int count=array.count;
  6. for (int i=0; i<count; i++) {
  7. id obj = [array objectAtIndex:i];
  8. NSLog(@"%i is %@",i,obj);
  9. }
  10. //2 快速遍历
  11. for(id obj in array){
  12. NSLog(@"%@",obj);
  13. }
  14. //3 利用block遍历,stop用于终止循环(break)
  15. [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  16. NSLog(@"%zi is %@",idx,obj);
  17. //如果索引为1时停止遍历
  18. if(idx ==1){
  19. *stop =YES;
  20. }
  21. }];
  22. //4利用迭代器遍历
  23. //获取迭代器
  24. NSEnumerator *enumerator = [array objectEnumerator];
  25. //获取反序迭代器
  26. //[array reverseObjectEnumerator];
  27. id obj =nil;
  28. while (obj =[enumerator nextObject]) {
  29. NSLog(@"%@",obj);
  30. };
  31. NSArray *array1 = [enumerator allObjects];//返回迭代器中的所有没被遍历过的元素。
  32. }

数组的排序:

  1. void arraySort(){
  2. NSArray *array = [NSArray arrayWithObjects:@"2",@"3",@"1",@"4", nil];
  3. //1
  4. //返回一个排好序的数组,原数组不变
  5. //指定元素的比较方法compare:
  6. NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
  7. //2 Student里定义排序方法
  8. Student *stu1 =[Student studentWithFirstname:@"张" andLastname:@"三"] ;
  9. Student *stu2 =[Student studentWithFirstname:@"李" andLastname:@"四"] ;
  10. Student *stu3 =[Student studentWithFirstname:@"王" andLastname:@"五"] ;
  11. Student *stu4 =[Student studentWithFirstname:@"赵" andLastname:@"六"] ;
  12. NSArray *array3 = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
  13. NSArray *array4 = [array3 sortedArrayUsingSelector:@selector(compareStudent:)];
  14. //3 利用block排序
  15. [array3 sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
  16. NSComparisonResult result=[obj1.firstname compare:obj1.firstname];
  17. if(result == NSOrderedSame){
  18. result = [obj1.lastname compare:obj1.lastname];
  19. }
  20. return result;
  21. }];
  22. //4
  23. NSSortDescriptor *fristnameAsc =[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
  24. NSSortDescriptor *lastnameDesc =[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:NO];
  25. NSArray *sort =[NSArray arrayWithObjects:fristnameAsc,lastnameDesc, nil];
  26. NSArray *array5= [array3 sortedArrayUsingDescriptors:sort];
  27. }

派生出新的数组

  1. void arrayNew(){
  2. NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
  3. NSArray *array2=[array arrayByAddingObject:@"d"];
  4. NSArray *array3=[array arrayByAddingObjectsFromArray:[array arrayByAddingObject:@"d"]];
  5. NSRange range =NSMakeRange(1, 2);
  6. NSArray *array4=[array subarrayWithRange:range];
  7. }

其它

  1. void arrayOther(){
  2. NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
  3. //用","连接数组中所有对象
  4. NSString *str =[array componentsJoinedByString:@","];
  5. //将一个数组持久化到文件中,文件内容是xml格式
  6. NSString *path=@"/Users/llw/tmp/test.xml";
  7. [array writeToFile:path atomically:YES];
  8. //从文件中读取。(文件内容有严格的格式要求)
  9. NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
  10. }

NSMutableArray

可变数组。

NSDictonary

字典,不可变的。

  1. void dictCreate(){
  2. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
  3. //最常用方法
  4. dict=[NSDictionary dictionaryWithObjectsAndKeys:
  5. @"v1",@"k1",
  6. @"v2",@"k2", nil];
  7. NSArray *objects =[NSArray arrayWithObjects:@"v1",@"v2",@"v3", nil];
  8. NSArray *keys =[NSArray arrayWithObjects:@"k1",@"k2",@"k3", nil];
  9. dict =[NSDictionary dictionaryWithObjects:objects forKeys:keys];
  10. }
  1. void dictUse(){
  2. NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
  3. @"v1",@"k1",
  4. @"v2",@"k2", nil];
  5. unsigned long count = dict.count;
  6. //是不可变的,只能取值,不能修改值
  7. id obj = [dict objectForKey:@"k2"];
  8. //write to file
  9. NSString *path=@"/Users/llw/tmp/test.txt";
  10. [dict writeToFile:path atomically:YES];
  11. NSArray *keys = [dict allKeys];
  12. NSArray *objects =[dict allValues];
  13. //notFoundMarker 表示 根据key找不到值时用notFoundMarker代替
  14. NSArray *objects2 = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2", nil] notFoundMarker:@"no found"];
  15. }

遍历

  1. void dictFor(){
  2. NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
  3. @"v1",@"k1",
  4. @"v2",@"k2", nil];
  5. //1
  6. for (id key in dict) {
  7. id value = [dict objectForKey:key];
  8. }
  9. //2 使用迭代器
  10. //分key迭代器 ,value迭代器
  11. NSEnumerator *enumer =[dict keyEnumerator];
  12. id key =nil;
  13. while (key = [enumer nextObject]) {
  14. id value = [dict objectForKey:key];
  15. }
  16. NSEnumerator *enumer1 =[dict objectEnumerator];
  17. //3 block,stop用于终止循环(break)
  18. [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  19. NSLog(@"[%@ %@]",key,obj);
  20. }];
  21. }

NSMutableDictionary

可变字典

NSSet

NSMutableSet

NSNumber

可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray,NSDictionary等集合中。

  1. void number(){
  2. NSNumber *number = [NSNumber numberWithInt:10];
  3. NSMutableArray *array =[NSMutableArray array];
  4. [array addObject:number];
  5. //取出来还是number,不会自动解包
  6. NSNumber *number1 =[array lastObject];
  7. int num= [number1 intValue];
  8. }

NSValue

NSNumber是NSValue的子类,但NSNumber只能包装基本数据类型,NSValue可以包装任意值,也就可以用NSValue包装结构体后加入NSArray,等集合中。

系统自带结构体

  1. void value(){
  2. CGPoint point =CGPointMake(10, 10);
  3. //将结构体包装成一个对象
  4. NSValue *value = [NSValue valueWithPoint:point];
  5. NSMutableArray *array =[NSMutableArray array];
  6. [array addObject:value];
  7. NSValue *value1 =[array lastObject];
  8. CGPoint point1 =[value1 pointValue];
  9. BOOL result = CGPointEqualToPoint(point,point1);
  10. }

自定义结构体

  1. typedef struct{
  2. int year;
  3. int month;
  4. int day;
  5. } Date;
  6. void value2(){
  7. Date date= {2013,4,7};
  8. //void * 代表任何指针
  9. //这里要传结构体的地址
  10. //根据结构体类型生成 对应的 类型描述字符串
  11. char *type =@encode(Date);
  12. NSValue * value = [NSValue value:&date withObjCType:type];
  13. //定义一个结构体变量
  14. Date date1;
  15. //取出包装好的结构体
  16. [value getValue:&date1 ];
  17. }

NSNull

集合中不能存放nil值,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么久可以使用NSNull,它也是NSObject的一个子类。

  1. //[NSNull null]返回的是同一个单例对象,所以n = n1
  2. NSNull *n =[NsNull null];
  3. NSNull *n1 =[NSNull null];

NSDate





3、Foundation

标签:

原文地址:http://www.cnblogs.com/mains/p/5204507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!