1.结构体
1>.NSRange(location, length); 从location这个位置开始计数长度为length
NSRange的创建方式:
NSRange r1 = {location, length}; // es: NSRange r1 = {2, 4}; 一般不这样写 NSRange r2 = {.location = 2, .length = 4} // 一般也不这样写 NSRange r3 = NSMakeRange(2, 4); // 必须掌握 es:查找某个字符串在str中的范围 如果找不到,length = 0,location = NSNotFound==-1 NSString *str = @"whb love lap"; NSRange range = [str rangeOfString:@"love"]; NSLog(@"location = %ld,length = %ld",range.location, range.length);
3>.NSSize/CGSize // 长度,宽度
NSSize s1 = CGSizeMake(11,25); // 长度为11,宽度为25的图形 NSSize s2 = NSMakeSize(11,25); CGSize s3 = NSMakeSize(11.25);
4>.CGRect r = CGRectMake(CGPoint, CGSize);
CGRect r1 = NSRectMake(CGPointZero, CGSizeMake(11,25)); 使用CGPointZero等的前提是添加CoreGraphics框架 // 表示原点 // CGPointZero == CGPointMake(0, 0) // 判断点是否在矩形框内: bool b2 = CGRectContainsPoint(CGRectMake(11,23,22,,33),CGPointMake(12,44)); 转换成字符串: NSString *str = NSStringFromPoint(p1); ········ *str = NSStringFromSize(s3); ········ *str = NSStringFromRect(r1);
2.NSString字符串的创建
1》. NSString *s1 = @"whblap" 2》. NSString *s2 = [[NSString alloc] initWithString:@"whb's age is 22"]; 3》. NSString *s3 = [[NSString alloc] initWithFormat:@"whb's age is %d",22]; 4》. C字符串 --> OC字符串 NSString *s4 = [[NSString alloc] initWithUTF8String:"whb's age is 22"]; OC字符串 --> C字符串 const char *cs = [s4 UTF8String]; 5》. NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/whblap/Desktop/document.m" encoding:NSUTF8StringEncoding error:nil]; // 遇到中文就可以用这种编码 6》.使用路径创建字符串 // URL : 资源路径 // 协议头://路径 // file:// // ftp:// NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/whblap/Desktop/1.txt"]; NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; // s6对象存放的字符串是1.txt里面的内容 PS: 一般都会有一个类方法跟对象方法配对 [NSURL URLWithString:<#(NSString *)#>]; [NSString stringWithFormat:@""]; [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
3.字符串的导出,写入到文件
[@"5月21号老妈生日,5月31号我家爱萍生日,whb记得给她过生日哦" writeToFile:@"/Users/whblap/Desktop/1.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSString *str = @" whb 记得哦"; NSURL *url = [NSURL fileURLWithPath:@"/User/whblap/Desktop/1.txt"]; [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
4.NSMutableString可变字符串
NSString : 不可变字符串 NSMutableString : 可变字符串
//?????? 查一下 如何将@“love” 插入到s1字 符串中间第三个 有没这种方法 NSMutableString *s1 = [NSMutableString stringWithFormat:@"whb lap"]; [s1 appendString:@" !!! "]; // 拼接字符串到s1的后面 NSString *s2 = [s1 stringByAppendingString:@" !!!"]; // 功能同上