OC基础语法学习:Foundation框架
来源:互联网 发布:怎么复制淘宝商品链接 编辑:程序博客网 时间:2024/05/20 16:34
常用结构体:
在Foundation中定义了很多常用结构体类型来简化我们的日常开发,这些结构体完全采用Objective-C定义,和我们自己定义的结构体没有任何区别,之所以由框架为我们提供完全是为了简化我们的开发。常用的结构体有NSRange、NSPoint、NSSize、NSRect等
#import <Foundation/Foundation.h>/*NSRange表示一个范围*/void test1(){ NSRange rg={3,5};//第一参数是起始位置第二个参数是长度 //NSRange rg; //rg.location=3; //rg.length=5; //NSRange rg={.location=3,.length=5}; //常用下面的方式定义 NSRange rg2=NSMakeRange(3,5);//使用NSMakeRange定义一个NSRange //打印NSRange可以使用Foundation中方法 NSLog(@"rg2 is %@", NSStringFromRange(rg2));//注意不能直接NSLog(@"rg2 is %@", rg2),因为rg2不是对象(准确的说%@是指针)而是结构体}/*NSPoint表示一个点*/void test2(){ NSPoint p=NSMakePoint(10, 15);//NSPoint其实就是CGPoint //这种方式比较常见 NSPoint p2=CGPointMake(10, 15); NSLog(NSStringFromPoint(p2));}/*NSSize表示大小*/void test3(){ NSSize s=NSMakeSize(10, 15);//NSSize其实就是CGSize //这种方式比较常见 CGSize s2=CGSizeMake(10, 15); NSLog(NSStringFromSize(s2));}/*NSRect表示一个矩形*/void test4(){ NSRect r=NSMakeRect(10, 5, 100, 200);//NSRect其实就是CGRect //这种方式比较常见 NSRect r2=CGRectMake(10, 5, 100, 200); NSLog(NSStringFromRect(r2));}int main(int argc, const char * argv[]) { @autoreleasepool { test1(); test2(); test3(); test4(); } return 0;}
不可变数组
//// main.m// FoundationFramework//// Created by Kenshin Cui on 14-2-16.// Copyright (c) 2014年 Kenshin Cui. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"void test1(){ //NSArray长度不可变所以初始化的时候就赋值,并且最后以nil结尾 //此外需要注意NSArray不能存放C语言的基础类型 NSObject *obj=[[NSObject alloc]init]; //NSArray *array1=[[NSArray alloc] initWithObjects:@"abc",obj,@"cde",@"opq", nil]; NSArray *array1=[NSArray arrayWithObjects:@"abc",obj,@"cde",@"opq",@25, nil]; NSLog(@"%zi",array1.count);//数组长度,结果:5 NSLog(@"%i",[array1 containsObject:@"cde"]);//是否包含某个对象,结果:1 NSLog(@"%@",[array1 lastObject]);//最后一个对象,结果:25 NSLog(@"%zi",[array1 indexOfObject:@"abc"]);//对象所在的位置:0 Person *person1=[Person personWithName:@"Kenshin"]; Person *person2=[Person personWithName:@"Kaoru"]; Person *person3=[Person personWithName:@"Rosa"]; NSArray *array2=[[NSArray alloc]initWithObjects:person1,person2,person3, nil]; [array2 makeObjectsPerformSelector:@selector(showMessage:) withObject:@"Hello,world!"];//执行所有元素的showMessage方法,后面的参数最多只能有一个 /*结果: My name is Kenshin,the infomation is "Hello,world!". My name is Kaoru,the infomation is "Hello,world!". My name is Rosa,the infomation is "Hello,world!". */}//数组的遍历void test2(){ NSObject *obj=[[NSObject alloc]init]; NSArray *array=[[NSArray alloc] initWithObjects:@"abc",obj,@"cde",@"opq",@25, nil]; //方法1 for(int i=0,len=array.count;i<len;++i){ NSLog(@"method1:index %i is %@",i,[array objectAtIndex:i]); } /*结果: method1:index 0 is abc method1:index 1 is <NSObject: 0x100106de0> method1:index 2 is cde method1:index 3 is opq method1:index 4 is 25 */ //方法2 for(id obj in array){ NSLog(@"method2:index %zi is %@",[array indexOfObject:obj],obj); } /*结果: method2:index 0 is abc method2:index 1 is <NSObject: 0x100602f00> method2:index 2 is cde method2:index 3 is opq method2:index 4 is 25 */ //方法3,利用代码块方法 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"method3:index %zi is %@",idx,obj); if(idx==2){//当idx=2时设置*stop为YES停止遍历 *stop=YES; } }]; /*结果: method3:index 0 is abc method3:index 1 is <NSObject: 0x100106de0> method3:index 2 is cde */ //方法4,利用迭代器 //NSEnumerator *enumerator= [array objectEnumerator];//获得一个迭代器 NSEnumerator *enumerator=[array reverseObjectEnumerator];//获取一个反向迭代器 //NSLog(@"all:%@",[enumerator allObjects]);//获取所有迭代对象,注意调用完此方法迭代器就遍历完了,下面的nextObject就没有值了 id obj2=nil; while (obj2=[enumerator nextObject]) { NSLog(@"method4:%@",obj2); } /*结果: method4:25 method4:opq method4:cde method4:<NSObject: 0x100106de0> method4:abc */}//数组派生出新的数组void test3(){ NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil]; NSArray *array2=[array arrayByAddingObject:@"4"];//注意此时array并没有变 NSLog(@"%@",array2); /*结果: ( 1, 2, 3, 4 ) */ NSLog(@"%@",[array2 arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"5",@"6", nil]]);//追加形成新的数组 /*结果: ( 1, 2, 3, 4, 5, 6 ) */ NSLog(@"%@",[array2 subarrayWithRange:NSMakeRange(1, 3)]);//根据一定范围取得生成一个新的数组 /*结果: ( 2, 3, 4 ) */ NSLog(@"%@",[array componentsJoinedByString:@","]);//数组连接,形成一个字符串 //结果:1,2,3 //读写文件 NSString *path=@"/Users/KenshinCui/Desktop/array.xml"; [array writeToFile:path atomically:YES]; NSArray *array3=[NSArray arrayWithContentsOfFile:path]; NSLog(@"%@",array3); /*结果: ( 1, 2, 3 ) */}//数组排序void test4(){ //方法1,使用自带的比较器 NSArray *array=[NSArray arrayWithObjects:@"3",@"1",@"2", nil]; NSArray *array2= [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",array2); /*结果: ( 1, 2, 3 ) */ //方法2,自己定义比较器 Person *person1=[Person personWithName:@"Kenshin"]; Person *person2=[Person personWithName:@"Kaoru"]; Person *person3=[Person personWithName:@"Rosa"]; NSArray *array3=[NSArray arrayWithObjects:person1,person2,person3, nil]; NSArray *array4=[array3 sortedArrayUsingSelector:@selector(comparePerson:)]; NSLog(@"%@",array4); /*结果: ( "name=Kaoru", "name=Kenshin", "name=Rosa" ) */ //方法3使用代码块 NSArray *array5=[array3 sortedArrayUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2) { return [obj2.name compare:obj1.name];//降序 }]; NSLog(@"%@",array5); /*结果: ( "name=Rosa", "name=Kenshin", "name=Kaoru" ) */ //方法4 通过描述器定义排序规则 Person *person4=[Person personWithName:@"Jack"]; Person *person5=[Person personWithName:@"Jerry"]; Person *person6=[Person personWithName:@"Tom"]; Person *person7=[Person personWithName:@"Terry"]; NSArray *array6=[NSArray arrayWithObjects:person4,person5,person6,person7, nil]; //定义一个排序描述 NSSortDescriptor *personName=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; NSSortDescriptor *accountBalance=[NSSortDescriptor sortDescriptorWithKey:@"account.balance" ascending:YES]; NSArray *des=[NSArray arrayWithObjects:personName,accountBalance, nil];//先按照person的name排序再按照account的balance排序 NSArray *array7=[array6 sortedArrayUsingDescriptors:des]; NSLog(@"%@",array7); /*结果: ( "name=Jack", "name=Jerry", "name=Terry", "name=Tom" ) */}int main(int argc, const char * argv[]) { test1(); test2(); test3(); test4(); return 0;}
可变数组
#import <Foundation/Foundation.h>#import "Person.h"void test1(){ Person *person1=[Person personWithName:@"Kenshin"]; Person *person2=[Person personWithName:@"Kaoru"]; Person *person3=[Person personWithName:@"Rosa"]; NSMutableArray *array1=[NSMutableArray arrayWithObjects:person1,person2,person3, nil]; NSLog(@"%@",array1); /*结果: ( "name=Kenshin", "name=Kaoru", "name=Rosa" ) */ Person *person4=[Person personWithName:@"Jack"];//此时person4的retainCount为1 [array1 addObject:person4];//添加一个元素,此时person4的retainCount为2 NSLog(@"%@",array1); /*结果: ( "name=Kenshin", "name=Kaoru", "name=Rosa", "name=Jack" ) */ [array1 removeObject:person3];//删除一个元素 NSLog(@"%@",array1); /*结果: ( "name=Kenshin", "name=Kaoru", "name=Jack" ) */ [array1 removeLastObject];//删除最后一个元素,//此时person4的retainCount为1 NSLog(@"%@",array1); /*结果: ( "name=Kenshin", "name=Kaoru" ) */ [array1 removeAllObjects];//删除所以元素 //注意当往数组中添加一个元素时会retain因此计数器+1,当从数组中移除一个元素时会release因此计数器-1 //当NSMutalbeArray对象release的时候会依次调用每一个对象的release}void test2(){ NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"1",@"3",@"2", nil]; NSLog(@"%@",array1); /*结果: ( 1, 3, 2 ) */ NSArray *array2= [array1 sortedArrayUsingSelector:@selector(compare:)];//注意这个方法没有修改array1 NSLog(@"%@",array1); /*结果: ( 1, 3, 2 ) */ NSLog(@"%@",array2); /*结果: ( 1, 2, 3 ) */ [array1 sortUsingSelector:@selector(compare:)];//这个方法会修改array1 NSLog(@"%@",array1); /*结果: ( 1, 2, 3 ) */}int main(int argc, const char * argv[]) { test1(); test2(); return 0;}
0 0
- OC基础语法学习:Foundation框架
- 黑马程序员---OC基础---Foundation框架学习
- 【OC语法】Foundation框架-NSObject
- 黑马程序员———OC基础语法(七)---Foundation框架
- IOS学习---OC基础学习(Foundation)
- iOS学习笔记:Object-C基础语法(OC介绍与Foundation初步)
- OC学习:Foundation框架中的常用类
- OC语法<4.1>:Foundation框架中的NSString类
- OC语法<4.2>:Foundation框架中的集合类-NSArray
- OC语法<4.3> Foundation框架中的集合类-NSDictionary
- OC语法<4.4> Foundation框架中的集合类-NSSet
- OC语法<4.6> Foundation框架中的-文件管理器
- OC语法<4.7> Foundation框架中的-日期操作
- 黑马程序员——OC基础学习(六)---Foundation框架的常用类
- oc Foundation框架
- OC:Foundation框架
- OC:Foundation框架 补充
- OC:Foundation框架 1212
- 使用百度地图——入门
- BIG-endian && little endian
- Android Fragment 真正的完全解析(上)
- srm 534
- Android Project中运行带有main函数的Java类
- OC基础语法学习:Foundation框架
- GRE阅读有哪些固定的答题思路?
- 朋友范LOGO故事
- 封装Linux下网络连通性检测函数
- 线程函数 pthread_detach()
- 一个数值计算中通常很有用的数值的定义和计算
- Cocoa Touch事件处理流程--响应者链
- style、currentStyle、getComputedStyle区别介绍
- LeetCode 之 Search for a Range — C++ 实现