標籤: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 }
Objective-C NSString 建立初始化的幾種方法