NSRange
表示一个范围
主要用来操作字符串
代码示例:
01 |
//NSRange是一个结构体,其中location是一个以0为开始的index,length是表示对象的长度。他们都是NSUInteger类型。 |
02 |
//定义如下 |
03 |
typedef struct _NSRange
{ |
04 |
NSUInteger
location; |
05 |
NSUInteger
length; |
06 |
}
NSRange; |
07 |
08 |
//NSUInteger的本质时unsigned
long类型 |
09 |
typedef unsigned long NSUInteger; |
10 |
11 |
NSRange
range = NSMakeRange(2,4); //需要记住的创建范围方式 |
12 |
13 |
NSString
*str = @ "i
love oc" ; |
14 |
//NSString的对象方法rangeOfString查找某个字符串在str中的范围 |
15 |
NSRange
range1 = [str rangeOfString:@ "ve
o" ]; //ve
o在str字符串中的范围 4 4 |
16 |
//如果找不到,location=NSNoFound=-1,length=0, |
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值往右面增加
代码示例:
01 |
//NSPoint和CGPoint相等 |
02 |
CGPoint
p1 = NSMakePoint(30, 40); |
03 |
CGPoint
p2 = CGPointMake(10, 10); //开发中这个最常用 |
04 |
05 |
//CGPointZero代表x和y坐标都为0-----(0,0) |
06 |
CGPoint
p3 = CGPointZero; |
07 |
08 |
//NSPoint的本质是CGPoint |
09 |
typedef CGPoint
NSPoint; |
10 |
11 |
//CGPoint的本质 |
12 |
struct CGPoint
{ |
13 |
CGFloat
x; |
14 |
CGFloat
y; |
15 |
}; |
16 |
typedef struct CGPoint
CGPoint; |
17 |
18 |
//比较两个点是否相同(x、y) |
19 |
BOOL result1
= CGPointEqualToPoint(CGPointZero, CGPointMake(1, 0)); //0 |
CGSize、NSSize
表示一个矩形尺寸,宽高
代码示例
01 |
//创建了两个矩形,s1的宽是100,高是60 |
02 |
CGSize
s1 = CGSizeMake(100, 60); |
03 |
NSSize
s2 = NSMakeSize(100, 50); |
04 |
CGSize
s3 = CGSizeZero; |
05 |
06 |
//CGSize的本质 |
07 |
struct CGSize
{ |
08 |
CGFloat
width; |
09 |
CGFloat
height; |
10 |
}; |
11 |
typedef struct CGSize
CGSize; |
12 |
13 |
//比较两个尺寸是否相同 |
14 |
BOOL result2
= CGSizeEqualToSize(CGSizeZero, CGSizeMake(0, 0)); //1 |
CGRect、NSRect
CGRect包含了一个按钮在手机屏幕上的位置,和按钮本身的宽和高
代码示例:
01 |
//Rect的本质 |
02 |
struct CGRect
{ |
03 |
CGPoint
origin; |
04 |
CGSize
size; |
05 |
}; |
06 |
typedef struct CGRect
CGRect; |
07 |
08 |
09 |
NSPoint
p1 = CGPointMake(10, 10); |
10 |
CGSize
s3 = NSMakeSize(200, 60); |
11 |
//创建了一个Rect |
12 |
CGRect
c1 = {p1, s1}; |
13 |
14 |
15 |
//将结构体转换成字符串 |
16 |
NSString
*str = NSStringFromPoint(p1); |
17 |
NSString
*str2 = NSStringFromSize(s1); |
18 |
NSString
*str3 = NSStringFromRect(r1); |
19 |
20 |
//然后打印 |
21 |
NSLog(@ "Point=%@,NSSize=%@,NSRect=%@" ,str,str2,str3); |
22 |
23 |
//多种Rect的创建方式 |
24 |
CGRect
c2 = {CGPointZero,CGSizeMake(100, 90)}; |
25 |
CGRect
c3 = {CGPointMake(10, 10),CGSizeZero}; |
26 |
CGRect
c4 = {{10,20},{200,100}}; |
27 |
28 |
//比较两个Rect是否相同 |
29 |
BOOL result3
= CGRectEqualToRect(c2, c3); |
30 |
31 |
//判断一个矩形中是否包含某个点,包含返回YES(1),不包含返回NO(0) |
32 |
BOOL result
= CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(100, 70)); //YES(1) |
33 |
34 |
//将结构体转换成字符串,方便打印 |
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.." ; |
2 |
3 |
NSString
*str3 = [[NSString alloc]initWithFormat:@ "age
is %d" ,10]; |
OC字符串和C字符串之间的转换
1 |
//
C字符串 -》 OC字符串 |
2 |
NSString
*str4 = [[NSString alloc] initWithUTF8String: "jack" ]; |
3 |
4 |
//
OC字符串 -》 C字符串 |
5 |
const char *str5=
[str4 UTF8String]; |
从文件或URL创建字符串
01 |
//URL:资源路径 |
02 |
//协议头://路径 |
03 |
//file:// |
04 |
//ftp:// |
05 |
//http://weibo.com/a.jpg |
06 |
//推荐用URL |
07 |
//也可以抓取网页的内容,比如www.baidu.com首页 |
08 |
//创建一个URL,只要是URL就必须指定好协议头 |
09 |
NSURL
*url = [[NSURL alloc] initWithString:@ "file:///Users/guankunluo/Desktop/1.txt" ]; |
10 |
11 |
//从文件读取,并创建字符串str6 |
12 |
NSString
*s = @ "/Users/guankunluo/Desktop/1.txt" ; |
13 |
NSString
*str6 = [[NSString alloc]initWithContentsOfFile:s encoding:NSUTF8StringEncoding error:nil]; |
14 |
15 |
//将字符串转换为file协议的地址 |
16 |
NSURL
*url2 = [NSURL fileURLWithPath:@ "/Users/guankunluo/Desktop/1.txt" ]; |
17 |
//从URL读取内容,并创建新的字符串str7 |
18 |
NSString
*str7 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; |
把字符串写入到文件(- writeToFile、 - writeToURL)
1 |
//把字符串写入到文件 |
2 |
NSString
*path = @ "/Users/guankunluo/Desktop/2.txt" ; |
3 |
//加入\n来代表换行 |
4 |
[@ "Jack
\n Jack" writeToFile:path
atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
5 |
6 |
//把字符串写入到URL |
7 |
NSURL
*url = [NSURL URLWithString:@ "file:///Users/guankunluo/Desktop/2.txt" ]; |
8 |
[@ "lalalalala" writeToURL:url
atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
NSMutableString(可变字符串)
创建一个可变的字符串
1 |
//创建一个NSMutableString,可变 |
2 |
NSMutableString
*str1 =[NSMutableString stringWithFormat:@ "age
is %d" ,10]; |
拼接字符串
1 |
//拼接内容到str1的结尾处,无返回值 |
2 |
[str1
appendString:@ "
11 12" ]; |
删除字符串
1 |
//根据范围把字符串中is删除,但是str1的字符串改变,范围也就改变了,所以不好 |
2 |
//[str1
deleteCharactersInRange:NSMakeRange(4,2)]; |
3 |
4 |
//应该根据rangeOfString方法先找出is的范围 |
5 |
NSRange
range = [str1 rangeOfString:@ "is" ]; |
6 |
7 |
//然后在把取得的范围传入删除字符串方法的参数中,这样以后再改变也会随之改变范围 |
8 |
[str1
deleteCharactersInRange:range]; |
NSArray(有序不可变集合)
集合只能存放OC对象,不能存放基本类型
创建NSArray
1 |
//集合只能存放OC对象,不能存放基本类型 |
2 |
//创建只有1个集合,并设置一个元素 |
3 |
NSArray
*array01 = [NSArray arrayWithObject:@ "abc" ]; |
4 |
5 |
//nil时集合元素的结束标记,不能删除 |
6 |
//集合中也不能定义nil元素 |
7 |
//NSArray
*array02 = [NSArray arrayWithObjects:@"abc",@"def", nil]; //不推荐 |
8 |
NSArray
*array02 = @[@ "abc" ,@ "def" ]; //推荐 |
获取NSArray的长度
1 |
//[array02
count]; |
2 |
3 |
//array02.count
返回的是unsigned long |
4 |
NSLog(@ "%ld" ,array02.count); |
访问集合
1 |
NSLog(@ "%@" ,[array02
objectAtIndex:1]); //不推荐 |
2 |
NSLog(@ "%@" ,array02[1]); //推荐 |
NSMutableArray(有序可变集合)
创建可变集合
1 |
//创建一个可变的空集合 |
2 |
NSMutableArray
*array = [NSMutableArray array]; |
3 |
4 |
//初始化可变集合时,添加元素 |
5 |
NSMutableArray
*array2 = [NSMutableArray arrayWithObjects: @ "sss" ,
@ "aaa" ,
@ "bbb" ,
nil]; |
6 |
7 |
//错误,@[]返回的时NSArray,所以不能用@[]创建可变集合NSMutableArray |
8 |
//NSMutableArray
*array3 = @[@"dd",@"dsd",@"dsd"]; |
添加元素
1 |
//给可变集合添加元素 |
2 |
[array
addObject:[[Person alloc] init]]; |
3 |
[array
addObject:@ "jack" ]; |
删除元素
01 |
//删除所有元素 |
02 |
[array2
removeAllObjects]; |
03 |
04 |
//删除指定元素 |
05 |
[array2
removeObject:@ "aaa" ]; |
06 |
07 |
//删除指定索引的元素 |
08 |
[array2
removeObjectAtIndex:1]; |
09 |
10 |
//删除最后一个元素 |
11 |
[array2
removeLastObject]; |
NSSet和NSMutableSet(无序不可变集合和可变集合)
NSSet 和 NSArray的对比
共同点:
1.都是集合,都能存放多个OC对象
2.只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、float等、结构体、枚举)
3.都有一个可变的子类,本身不可变不同点:
1.NSArray有顺序,NSSet无顺序
NSSet
1 |
//错误,没有意义,创建了一个空的set集合,而且Set是不可变的 |
2 |
//NSSet
*s = [NSSet set]; |
3 |
4 |
NSSet
*s = [NSSet setWithObjects: @ "aaa" ,
@ "bbb" ,
@ "ccc" ,
@ "ddd" ,
nil]; |
5 |
6 |
//由于NSSet是无序的,所以只能随机取出元素 |
7 |
//随机取出元素 |
8 |
NSString
*str = [s anyObject]; |
NSMutableSet
01 |
NSMutableSet
*s = [NSMutableSet set]; |
02 |
03 |
//添加元素 |
04 |
[s
addObject:@ "hack" ]; |
05 |
[s
addObject:@ "haha" ]; |
06 |
07 |
//删除所有元素 |
08 |
[s
removeAllObjects]; |
09 |
10 |
//删除指定元素 |
11 |
[s
removeObject:@ "haha" ]; |
NSDictionary和NSMutableDictionary(可变和不可变的字典集合)
字典:相当于java中的map
字典是无序的
key—-》value
索引—-》文字内容
Dictionary中存储的是键值对
Dictionary中的key不能重复,value可以重复
NSDictionary(不可变字典集合)
01 |
//NSDictionary---不可变的字典、无序的 |
02 |
//创建了一个字典,但是只存储了一个键值对 |
03 |
NSDictionary
*dict = [NSDictionary dictionaryWithObject:@ "luoguankun" forKey:@ "name" ]; |
04 |
05 |
//通过key获取value |
06 |
NSString
*name = [dict objectForKey:@ "name" ]; |
07 |
NSLog(@ "%@" ,name); //luoguankun |
08 |
//推荐这样访问字典中的value |
09 |
id
obj = dict[@ "name" ]; |
10 |
11 |
//通过传入2个不可变数组NSArray来初始化Dictionary字典集合 |
12 |
NSArray
*key = @[@ "name" ,@ "address" ]; |
13 |
NSArray
*value = @[@ "罗冠坤" ,@ "北京" ]; |
14 |
NSDictionary
*dict2 = [NSDictionary dictionaryWithObjects:value forKeys:key]; |
15 |
//如果传入的key没有找到,就返回null,不报错 |
16 |
NSLog(@ "%@" ,[dict2
objectForKey:@ "address" ]); //北京 |
17 |
18 |
//下面的写法可读性很差 |
19 |
NSDictionary
*dict3 = [NSDictionary dictionaryWithObjectsAndKeys: |
20 |
@ "luoguankun" ,@ "name" , |
21 |
@ "北京" ,@ "address" ,nil]; |
22 |
23 |
//推荐使用这种方式初始化创建字典 |
24 |
NSDictionary
*dict4 = @{@ "name" :
@ "luoguankun" ,@ "address" :
@ "北京" }; |
25 |
26 |
//返回键值对的个数,不是key+value的个数 |
27 |
int count
= dict4.count; |
NSMutableDictionary(可变字典集合)
01 |
//错误!@{}返回不可变的Dictionary |
02 |
//NSMutableDictionary
*dict = @{@"name",@"luoguankun"}; |
03 |
04 |
NSMutableDictionary
*dict = [NSMutableDictionary dictionary]; |
05 |
06 |
//添加键值对 |
07 |
[dict
setObject:@ "luoguankun" forKey:@ "name" ]; |
08 |
[dict
setObject:@ "北京" forKey:@ "address" ]; |
09 |
10 |
//当设置了重复的key时,会覆盖以前的,所以dictionary中的key是唯一的,value可以重复 |
11 |
[dict
setObject:@ "luo" forKey:@ "name" ]; |
12 |
13 |
NSLog(@ "%@" ,dict[@ "name" ]); //luo |
14 |
15 |
//删除某个key所指向的键值对 |
16 |
[dict
removeObjectForKey:@ "address" ]; |
Dictionary字典集合的遍历
01 |
NSDictionary
*dict = @{@ "name" :
@ "luoguankun" ,@ "address" :@ "北京" }; |
02 |
03 |
//通过for循环遍历NSDictionary,不推荐 |
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); |
08 |
} |
09 |
10 |
//推荐使用这种遍历方式,通过block遍历集合 |
11 |
[dict
enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{ |
12 |
NSLog(@ "%@-%@" ,key,obj); |
13 |
14 |
//打印一次就停止 |
15 |
*stop
= YES; |
16 |
}]; |
遍历集合
方式一(for)
1 |
//NSArray快速遍历 |
2 |
NSArray
*array1 = @[@ "a" ,@ "b" ,@ "c" ,@ "d" ,@ "f" ,@ "g" ]; |
3 |
4 |
//普通的for循环 |
5 |
for ( int i
= 0; i < array.count; i++) |
6 |
{ |
7 |
NSLog(@ "%@" ,array[i]); |
8 |
} |
方式二(for in)
1 |
//很像java中的foreach |
2 |
//id
obj 代表着集合中的每一个元素 |
3 |
for (id
obj in array1) |
4 |
{ |
5 |
//找出obj元素在数组中的位置 |
6 |
unsigned long i
= [array1 indexOfObject:obj]; |
7 |
8 |
NSLog(@ "%ld=%@" ,i,obj); |
9 |
} |
方式三(block)推荐
01 |
//
每遍历到一个元素,就会调用一次block |
02 |
//
并且把当前元素和索引位置当做参数传给block |
03 |
//
stop的作用相当于break,终止循环 |
04 |
//
设置为YES,就停止循环 |
05 |
//obj代表当前元素对象 |
06 |
//idx代表元素下标 |
07 |
[array1
enumerateObjectsUsingBlock: |
08 |
^(id
obj, NSUInteger idx, BOOL *stop)
{ |
09 |
NSLog(@ "%ld
= %@" ,
idx,obj); |
10 |
11 |
if (idx
== 2) |
12 |
{ |
13 |
*stop
= YES; |
14 |
} |
15 |
} |
16 |
]; |
17 |
18 |
//block遍历集合的本质 |
19 |
void ^(myblock)(id,
NSUInteger, BOOL *)
= ^(id obj, NSUInteger idx, BOOL *step) |
20 |
{ |
21 |
NSLog(@ "%ld
- %@" ,
idx, obj); |
22 |
23 |
if (idx
== 2) { |
24 |
//停止遍历 |
25 |
*step
= YES; |
26 |
} |
27 |
}; |
28 |
29 |
30 |
for ( int i
= 0; i < array1.count; i++) { |
31 |
32 |
//用来标记是否需要停止遍历 |
33 |
BOOL isStop
= NO; |
34 |
35 |
//取出元素 |
36 |
id
obj = array1[i]; |
37 |
38 |
myblock(obj,
i, &isStop); |
39 |
40 |
if (isStop)
{ |
41 |
break ; |
42 |
} |
43 |
} |
练习—计算代码行数
001 |
#import
<Foundation/Foundation.h> |
002 |
003 |
NSUInteger
codeLineCount(NSString *); |
004 |
005 |
/* |
006 |
考察NSString、NSArray的使用 |
007 |
NSFileManager |
008 |
*/ |
009 |
int main( int argc, const char *
argv[]) |
010 |
{ |
011 |
012 |
/* |
013 |
分割字符串 |
014 |
NSString
*str = @"jack-rose- jim-jake"; |
015 |
NSArray
*array = [str componentsSeparatedByString:@"-"]; |
016 |
[array
enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { |
017 |
NSLog(@"%@",obj); |
018 |
}]; |
019 |
*/ |
020 |
021 |
022 |
NSUInteger
count = codeLineCount(@ "/Users/guankunluo/Documents/C_Files/" ); |
023 |
NSLog(@ "该路径下所有文件共计:%ld行" ,count); |
024 |
025 |
return 0; |
026 |
} |
027 |
028 |
029 |
/* |
030 |
path:文件全路径 |
031 |
返回值:代码行数 |
032 |
*/ |
033 |
NSUInteger
codeLineCount(NSString *filePath) |
034 |
{ |
035 |
036 |
//1.获取文件管理者,它是单例类 |
037 |
NSFileManager
*mgr = [NSFileManager defaultManager]; |
038 |
039 |
//2.判断path是文件夹还是文件路径,返回BOOL |
040 |
BOOL dir
= NO; //用来标记是否为文件夹 |
041 |
//这个路径是否存在,等于是返回了两个值,一个是方法返回值,一个是传进去的dir |
042 |
BOOL exist
= [mgr fileExistsAtPath:filePath isDirectory:&dir]; //这个方法会改变dir,因为传得是地址 |
043 |
044 |
//3.如果路径不存在,就直接返回0 |
045 |
if (!exist)
{ |
046 |
NSLog(@ "路径不存在" ); |
047 |
return 0; |
048 |
} |
049 |
050 |
int count
= 0; |
051 |
052 |
//说明路径存在 |
053 |
//判断是一个文件夹,还是是一个文件 |
054 |
if (dir)
{ |
055 |
056 |
//NSLog(@"是一个文件夹"); |
057 |
058 |
//装着当前文件夹下的所有内容(文件夹、文件) |
059 |
NSArray
*array = [mgr contentsOfDirectoryAtPath:filePath error:nil]; |
060 |
//打印出文件夹中的文件列表 |
061 |
//NSLog(@"%@",array); |
062 |
063 |
064 |
//根据文件列表,拼接出全路径 |
065 |
for (NSString
*filename in array) { |
066 |
NSString
*fullPath = [NSString stringWithFormat:@ "%@%@" ,filePath,filename]; |
067 |
count
+= codeLineCount(fullPath); |
068 |
} |
069 |
070 |
} else { |
071 |
072 |
//NSLog(@"是一个文件"); |
073 |
074 |
//4.获取文件的拓展名,并且转换成小写 |
075 |
NSString
*extension = [[filePath pathExtension] lowercaseString]; |
076 |
if (![extension
isEqualToString:@ "h" ] |
077 |
&&
![extension isEqualToString:@ "m" ] |
078 |
&&
![extension isEqualToString:@ "c" ]) |
079 |
{ |
080 |
//代表文件拓展名,不是m,也不是h,也不是c |
081 |
return 0; |
082 |
} |
083 |
084 |
//加载文件内容 |
085 |
NSString
*content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; |
086 |
087 |
//按指定字符串,分割字符串,返回集合 |
088 |
NSArray
*array = [content componentsSeparatedByString:@ "\n" ]; |
089 |
090 |
//控制输出的内容,把根路径换掉 |
091 |
NSRange
range = [filePath rangeOfString:@ "/Users/guankunluo/Documents/C_Files/" ]; |
092 |
//替换字符串 |
093 |
NSString
*str = [filePath stringByReplacingCharactersInRange:range withString:@ "" ]; |
094 |
095 |
NSLog(@ "%@-%ld" ,str,array.count); |
096 |
097 |
return array.count; |
098 |
} |
099 |
return count; |
100 |
} |