NSRange
表示一个范围
主要用来操作字符串
代码示例:
03 | typedef struct _NSRange { |
09 | typedef unsigned long NSUInteger; |
11 | NSRange range = NSMakeRange(2,4); |
13 | NSString *str = @ "i love oc" ; |
15 | NSRange range1 = [str rangeOfString:@ "ve o" ]; |
17 | NSRange range2 = [str rangeOfString:@ "java" ]; |
18 | NSLog(@ "\nlocation=%ld,length=%ld" ,range1.location,range1.length); |
CGPoint、NSPoint
通过设置x坐标和y坐标来设置某个点
IOS设备的x和y从左上角开始,y值往下面增加,x值往右面增加
代码示例:
02 | CGPoint p1 = NSMakePoint(30, 40); |
03 | CGPoint p2 = CGPointMake(10, 10); |
06 | CGPoint p3 = CGPointZero; |
09 | typedef CGPoint NSPoint; |
16 | typedef struct CGPoint CGPoint; |
19 | BOOL result1 = CGPointEqualToPoint(CGPointZero, CGPointMake(1, 0)); |
CGSize、NSSize
表示一个矩形尺寸,宽高
代码示例
02 | CGSize s1 = CGSizeMake(100, 60); |
03 | NSSize s2 = NSMakeSize(100, 50); |
04 | CGSize s3 = CGSizeZero; |
11 | typedef struct CGSize CGSize; |
14 | BOOL result2 = CGSizeEqualToSize(CGSizeZero, CGSizeMake(0, 0)); |
CGRect、NSRect
CGRect包含了一个按钮在手机屏幕上的位置,和按钮本身的宽和高
代码示例:
06 | typedef struct CGRect CGRect; |
09 | NSPoint p1 = CGPointMake(10, 10); |
10 | CGSize s3 = NSMakeSize(200, 60); |
16 | NSString *str = NSStringFromPoint(p1); |
17 | NSString *str2 = NSStringFromSize(s1); |
18 | NSString *str3 = NSStringFromRect(r1); |
21 | NSLog(@ "Point=%@,NSSize=%@,NSRect=%@" ,str,str2,str3); |
24 | CGRect c2 = {CGPointZero,CGSizeMake(100, 90)}; |
25 | CGRect c3 = {CGPointMake(10, 10),CGSizeZero}; |
26 | CGRect c4 = {{10,20},{200,100}}; |
29 | BOOL result3 = CGRectEqualToRect(c2, c3); |
32 | BOOL result = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(100, 70)); |
35 | NSString *str = NSStringFromPoint(p1); |
36 | NSString *str2 = NSStringFromSize(s1); |
37 | NSString *str3 = NSStringFromRect(r1); |
38 | NSLog(@ "Point=%@,NSSize=%@,NSRect=%@" ,str,str2,str3); |
NSString(不可变字符串)
字符串创建(initWithFormat)
1 | NSString *str1 = @ "Something string.." ; |
3 | NSString *str3 = [[NSString alloc]initWithFormat:@ "age is %d" ,10]; |
OC字符串和C字符串之间的转换
2 | NSString *str4 = [[NSString alloc] initWithUTF8String: "jack" ]; |
5 | const char *str5= [str4 UTF8String]; |
从文件或URL创建字符串
09 | NSURL *url = [[NSURL alloc] initWithString:@ "file:///Users/guankunluo/Desktop/1.txt" ]; |
12 | NSString *s = @ "/Users/guankunluo/Desktop/1.txt" ; |
13 | NSString *str6 = [[NSString alloc]initWithContentsOfFile:s encoding:NSUTF8StringEncoding error:nil]; |
16 | NSURL *url2 = [NSURL fileURLWithPath:@ "/Users/guankunluo/Desktop/1.txt" ]; |
18 | NSString *str7 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; |
把字符串写入到文件(- writeToFile、 - writeToURL)
2 | NSString *path = @ "/Users/guankunluo/Desktop/2.txt" ; |
4 | [@ "Jack \n Jack" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
7 | NSURL *url = [NSURL URLWithString:@ "file:///Users/guankunluo/Desktop/2.txt" ]; |
8 | [@ "lalalalala" writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
NSMutableString(可变字符串)
创建一个可变的字符串
2 | NSMutableString *str1 =[NSMutableString stringWithFormat:@ "age is %d" ,10]; |
拼接字符串
2 | [str1 appendString:@ " 11 12" ]; |
删除字符串
5 | NSRange range = [str1 rangeOfString:@ "is" ]; |
8 | [str1 deleteCharactersInRange:range]; |
NSArray(有序不可变集合)
集合只能存放OC对象,不能存放基本类型
创建NSArray
3 | NSArray *array01 = [NSArray arrayWithObject:@ "abc" ]; |
8 | NSArray *array02 = @[@ "abc" ,@ "def" ]; |
获取NSArray的长度
4 | NSLog(@ "%ld" ,array02.count); |
访问集合
1 | NSLog(@ "%@" ,[array02 objectAtIndex:1]); |
2 | NSLog(@ "%@" ,array02[1]); |
NSMutableArray(有序可变集合)
创建可变集合
2 | NSMutableArray *array = [NSMutableArray array]; |
5 | NSMutableArray *array2 = [NSMutableArray arrayWithObjects: @ "sss" , @ "aaa" , @ "bbb" , nil]; |
添加元素
2 | [array addObject:[[Person alloc] init]]; |
3 | [array addObject:@ "jack" ]; |
删除元素
02 | [array2 removeAllObjects]; |
05 | [array2 removeObject:@ "aaa" ]; |
08 | [array2 removeObjectAtIndex:1]; |
11 | [array2 removeLastObject]; |
NSSet和NSMutableSet(无序不可变集合和可变集合)
NSSet 和 NSArray的对比
共同点:
1.都是集合,都能存放多个OC对象
2.只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、float等、结构体、枚举)
3.都有一个可变的子类,本身不可变
不同点:
1.NSArray有顺序,NSSet无顺序
NSSet
4 | NSSet *s = [NSSet setWithObjects: @ "aaa" , @ "bbb" , @ "ccc" , @ "ddd" , nil]; |
8 | NSString *str = [s anyObject]; |
NSMutableSet
01 | NSMutableSet *s = [NSMutableSet set]; |
04 | [s addObject:@ "hack" ]; |
05 | [s addObject:@ "haha" ]; |
11 | [s removeObject:@ "haha" ]; |
NSDictionary和NSMutableDictionary(可变和不可变的字典集合)
字典:相当于java中的map
字典是无序的
key—-》value
索引—-》文字内容
Dictionary中存储的是键值对
Dictionary中的key不能重复,value可以重复
NSDictionary(不可变字典集合)
03 | NSDictionary *dict = [NSDictionary dictionaryWithObject:@ "luoguankun" forKey:@ "name" ]; |
06 | NSString *name = [dict objectForKey:@ "name" ]; |
09 | id obj = dict[@ "name" ]; |
12 | NSArray *key = @[@ "name" ,@ "address" ]; |
13 | NSArray *value = @[@ "罗冠坤" ,@ "北京" ]; |
14 | NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:value forKeys:key]; |
16 | NSLog(@ "%@" ,[dict2 objectForKey:@ "address" ]); |
19 | NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys: |
20 | @ "luoguankun" ,@ "name" , |
21 | @ "北京" ,@ "address" ,nil]; |
24 | NSDictionary *dict4 = @{@ "name" : @ "luoguankun" ,@ "address" : @ "北京" }; |
27 | int count = dict4.count; |
NSMutableDictionary(可变字典集合)
04 | NSMutableDictionary *dict = [NSMutableDictionary dictionary]; |
07 | [dict setObject:@ "luoguankun" forKey:@ "name" ]; |
08 | [dict setObject:@ "北京" forKey:@ "address" ]; |
11 | [dict setObject:@ "luo" forKey:@ "name" ]; |
13 | NSLog(@ "%@" ,dict[@ "name" ]); |
16 | [dict removeObjectForKey:@ "address" ]; |
Dictionary字典集合的遍历
01 | NSDictionary *dict = @{@ "name" : @ "luoguankun" ,@ "address" :@ "北京" }; |
04 | NSArray *keys = [dict allKeys]; |
05 | for ( int i = 0; i < keys.count ; i++) { |
06 | NSString *value = [dict objectForKey:keys[i]]; |
07 | NSLog(@ "%@---%@" ,keys[i],value); |
11 | [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { |
12 | NSLog(@ "%@-%@" ,key,obj); |
遍历集合
方式一(for)
2 | NSArray *array1 = @[@ "a" ,@ "b" ,@ "c" ,@ "d" ,@ "f" ,@ "g" ]; |
5 | for ( int i = 0; i < array.count; i++) |
方式二(for in)
6 | unsigned long i = [array1 indexOfObject:obj]; |
8 | NSLog(@ "%ld=%@" ,i,obj); |
方式三(block)推荐
07 | [array1 enumerateObjectsUsingBlock: |
08 | ^(id obj, NSUInteger idx, BOOL *stop) { |
09 | NSLog(@ "%ld = %@" , idx,obj); |
19 | void ^(myblock)(id, NSUInteger, BOOL *) = ^(id obj, NSUInteger idx, BOOL *step) |
21 | NSLog(@ "%ld - %@" , idx, obj); |
30 | for ( int i = 0; i < array1.count; i++) { |
38 | myblock(obj, i, &isStop); |
练习—计算代码行数
001 | #import <Foundation/Foundation.h> |
003 | NSUInteger codeLineCount(NSString *); |
009 | int main( int argc, const char * argv[]) |
022 | NSUInteger count = codeLineCount(@ "/Users/guankunluo/Documents/C_Files/" ); |
023 | NSLog(@ "该路径下所有文件共计:%ld行" ,count); |
033 | NSUInteger codeLineCount(NSString *filePath) |
037 | NSFileManager *mgr = [NSFileManager defaultManager]; |
042 | BOOL exist = [mgr fileExistsAtPath:filePath isDirectory:&dir]; |
059 | NSArray *array = [mgr contentsOfDirectoryAtPath:filePath error:nil]; |
065 | for (NSString *filename in array) { |
066 | NSString *fullPath = [NSString stringWithFormat:@ "%@%@" ,filePath,filename]; |
067 | count += codeLineCount(fullPath); |
075 | NSString *extension = [[filePath pathExtension] lowercaseString]; |
076 | if (![extension isEqualToString:@ "h" ] |
077 | && ![extension isEqualToString:@ "m" ] |
078 | && ![extension isEqualToString:@ "c" ]) |
085 | NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; |
088 | NSArray *array = [content componentsSeparatedByString:@ "\n" ]; |
091 | NSRange range = [filePath rangeOfString:@ "/Users/guankunluo/Documents/C_Files/" ]; |
093 | NSString *str = [filePath stringByReplacingCharactersInRange:range withString:@ "" ]; |
095 | NSLog(@ "%@-%ld" ,str,array.count); |