The difference between NSBundle and Nsurl:
In the project to add a file, this routine added is Aa.txt, the content of the file is Baidu: www.baidu.com, now to use NSBundle and Nsurl respectively to get the content, the code is as follows:
1 //Read File contents2 //Method 1: Read by file path3NSString *pathbundle = [[NSBundle mainbundle]pathforresource:@"AA"OfType:@"txt"];4NSString *outstringbundle =[NSString stringwithcontentsoffile:pathbundle encoding:nsutf8stringencoding Error:nil];5 6 //Method 2: Read by URL7Nsurl *pathurl = [[NSBundle mainbundle]urlforresource:@"AA"Withextension:@"txt"Subdirectory:nil];8NSString *outstringurl =[NSString stringwithcontentsofurl:pathurl encoding:nsutf8stringencoding Error:nil];9 TenNSLog (@"%@\n////////\n%@", Outstringbundle,outstringurl);
The output results are as follows:
1 $---£ º02.939 sandbox mechanism and file path [11786: 518929] Baidu: www.baidu.com2////////3 Baidu: www.baidu.com
iOS Development-ui Learn the difference between-nsbundle and nshomedirectory