首页 > 代码库 > Objective-C NSString 创建初始化的几种方法
Objective-C NSString 创建初始化的几种方法
1 #import <Foundation/Foundation.h>
2
3 int main(int argc,const char *argv[])
4 {
5
6 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
7 //从文件获取字符串 编码、错误 参数
8 NSStringEncoding encoding = NSUTF8StringEncoding;
9 NSError *error = [[NSError alloc]init];
10
11 //定义初始化字符串1
12 NSString *str1 = @"wxiao";
13 //定义初始化字符串2
14 NSString *str2 = [NSString stringWithString:@"xiaow"];
15 //定义初始化字符串3
16 NSString *str3 = [[NSString alloc] initWithString:@"objc"];
17 //定义初始化字符串4
18 NSString *str4 = [NSString stringWithFormat:@"%@ and %@",str1,str3];
19
20 //定义初始化字符串5
21 NSString *str5 = [NSString stringWithContentsOfFile:@"test" encoding:encoding error:&error];
22
23 NSLog(@"%@",str1);
24 NSLog(@"%@",str2);
25 NSLog(@"%@",str3);
26 NSLog(@"%@",str4);
27 NSLog(@"%@",str5);
28
29 [pool drain];
30
31 return 0;
32 }
2
3 int main(int argc,const char *argv[])
4 {
5
6 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
7 //从文件获取字符串 编码、错误 参数
8 NSStringEncoding encoding = NSUTF8StringEncoding;
9 NSError *error = [[NSError alloc]init];
10
11 //定义初始化字符串1
12 NSString *str1 = @"wxiao";
13 //定义初始化字符串2
14 NSString *str2 = [NSString stringWithString:@"xiaow"];
15 //定义初始化字符串3
16 NSString *str3 = [[NSString alloc] initWithString:@"objc"];
17 //定义初始化字符串4
18 NSString *str4 = [NSString stringWithFormat:@"%@ and %@",str1,str3];
19
20 //定义初始化字符串5
21 NSString *str5 = [NSString stringWithContentsOfFile:@"test" encoding:encoding error:&error];
22
23 NSLog(@"%@",str1);
24 NSLog(@"%@",str2);
25 NSLog(@"%@",str3);
26 NSLog(@"%@",str4);
27 NSLog(@"%@",str5);
28
29 [pool drain];
30
31 return 0;
32 }
Objective-C NSString 创建初始化的几种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。