1 //字符串的创建 2 NSString *s1 = @"Rose"; 3 4 // 作用相当于s1,基本不用 5 // 警告:Using ‘initWithString:‘ with a literal is redundant 6 // 用“initWithString:“是多余的 7 NSString *s2 = [[NSString alloc] initWithString:@"Rose"]; 8 9 10 NSString *s3 = [[NSString alloc]initWithFormat:@"age is %d",20]; 11 12 // C字符串——>OC字符串 13 // 注意"jack"前不要加@否则报错Implicit conversion of an Objective-C pointer to ‘const char *‘ is disallowed with ARC 14 // 隐式转换的objective - c指针到弧‘const char *’无效 15 NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"]; 16 // OC字符串——>c字符串 17 const char *cs = [s4 UTF8String]; 18 19 // NSUTF8StringEncoding 用到中文就可以用这种编码 20 NSString *s5 = [[NSString alloc]initWithContentsOfFile:@"/Users/scjy/Desktop/刘二龙练习/OC/作业/作业/图像面积/图像面积/main.m" encoding:NSUTF8StringEncoding error:nil]; 21 22 // URL :资源路径 23 // 协议头://路径 24 // file:// 25 // ftp:// 26 27 NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/scjy/Desktop/刘二龙练习/OC/作业/作业/图像面积/图像面积/main.m"]; 28 NSString *s6 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 29 30 NSLog(@"s6:%@",s6); 31 32 /* 33 一般都有一个类方法跟对象方法配对 34 [ NSURL URLWithString:<#(NSString *)#>]; 35 [NSString stringWithFormat:<#(NSString *), ...#>]; 36 [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>]; 37 38 */
时间: 2024-09-29 19:42:50