————————java培训、Andoroid培训、IOS培训、.Net培训、期待与您交流!————————
NSRange 范围
计算字符串@“i love oc”的范围
NSRange r=NSMakeRange(2,4);
查找某个字符串在str中的范围
NSRange range=[str rangeOfString:@"love"];
NSPoint/CGPoint 坐标
CGPoint p=CGPointMake(10, 20);
NSPoint p2=NSMakePoint(20, 20);
//比较两个点是否相同
CGPointEqualToPoint(p, p2);
//NSPoint/CGPoint转为字符串
NSString *str3=NSStringFromPoint(p);
NSSize/CGSize 尺寸
NSSize s=NSMakeSize(100, 20);
CGSize s2=CGSizeMake(200,200);
//比较矩形面积是否相等
CGSizeEqualToSize(s, s2);
//NSSize/CGSize转为字符串
NSString *str1=NSStringFromSize(s);
NSRect/CGRect 区域面积
CGRect r1=CGRectMake(10,20,100,20);
CGRect r3={p,s};
//CGPointZero表示原点
CGRect r4={CGPointZero,s};
//比较是否在该区域
CGRectContainsRect(r1, r2);
CGRectContainsPoint(CGRectMake(10, 20, 3, 8),CGPointMake(3, 6));
//NSRect/CGRect转为字符串
NSString *str2=NSStringFromRect(r1);
NSString 字符串
//字符串创建
NSString *s = @"jim";
NSString *s2 = [[NSString alloc] initWithFormat:@"age is %d",10];
//C字符串转OC字符串
NSString *s3=[[NSString alloc] initWithUTF8String:"jack"];
//OC字符串转C字符串
char *s4 = [s3 UTF8String];
//NSUTF8StringEncoding 中文格式编码
NSString *s5 = [[NSString alloc]initWithContentsOfFile:@"/Users/tjl/Dektop/1.txt" encoding:NSUTF8StringEncoding error:nil];
//URL:资源路径
协议头:http
//file:本地文件
/ftp:
NSURL *url=[[NSURL alloc] initWithString:@"http:www.baidu.com"];
NSString *s6 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s6);//最常用
//类方法转换
[NSURL URLWithString:s];
[NSString stringWithContentsOfFile:s5 encoding:NSUTF8StringEncoding error:nil];
[NSString stringWithFormat:s];
//文件资源
[NSURL fileURLWithPath:s];
//字符串写入文件
[@"hello world" writeToFile:@"/Users/tjl/Desktop/1.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str = @"hello world";
[str writeToURL:@"http://www.baidu.com" atomically:(YES) encoding:NSUTF8StringEncoding error:nil];
NSMutableString 可变的OC字符串
NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is 10"];
//拼接字符串
[s1 appendFormat:@"22 24"];
//删除指定的内容,先获取指定字符串范围,再查找
NSRange range=[s1 rangeOfString:@"is"];
[s1 deleteCharactersInRange:range];