沙盒和bundle 的理解

NSBundle is used to access resources within your application itself: that is, everything inside YourApp.app. The documentsDirectory is a location outside of your app -- it's in the "home directory" which is part of your app sandbox, and which is analogous to your user home directory on the Mac. 


进入响应文件夹,

如何进入?如图所示:



或者这样


在“前往文件夹”中输入想前往的文件夹

打开后你所见到的就是沙盒文件夹。

也就是说:你通过打开沙盒进入了沙盒文件夹。

新概念:Bundle

Bundle=.app文件,

下面我们来看一下,Bundle中有什么内容:

点击.app文件,右键打开包内容:

Bundle是下面沙盒中的四个文件中的哪个?

Bundle是四个中的.app文件


 



打开前,可将沙盒文件夹下的其他文件夹打开看看:



注意:你在这里看到的这个所谓的沙盒文件夹,并不是正真意义上的沙盒。因为他是模拟器模拟的。


所以你可以这样

打开包内容可见:其中我们把Bundle中的这些文件的各自的路径统称为mainBundle;

和沙盒之于应用程序自己数据文件和偏好设置文件,犹mainBundle之于Bundle;

不同的是沙盒和mainBundle区别在于:

mainBundle只关注应用程序,而沙盒也关注应用程序的数据和偏好设置;

//在博主看来mianBundle也是沙盒,不过是沙盒中的沙盒;


  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     NSString *homePath = [[NSString alloc] initWithFormat:@"%@",NSHomeDirectory()];  
  4.     NSLog(@"%@",homePath);  
  5.     NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES) objectAtIndex:0];  
  6.     NSLog(@"%@",docPath);  
  7.     NSString *tmpPath = NSTemporaryDirectory();  
  8.     NSLog(@"%@",tmpPath);  
  9.     //偏好设置  
  10.     [[NSUserDefaults standardUserDefaults] setFloat:23.4 forKey:@"float"];  
  11.     NSString *appPath = [[NSBundle mainBundle] resourcePath];  
  12.     NSLog(@"%@",appPath);  
  13.     NSString *bundlePath = [[NSBundle mainBundle ] bundlePath];  
  14.     NSLog(@"%@",bundlePath);  
  15.     NSString *execPath = [[NSBundle mainBundle ] executablePath];  
  16.     NSLog(@"%@",execPath);  
  17.     NSString *filePath = [[NSBundle mainBundle ] pathForResource:@"Chen_Yilong" ofType:@"rtf"];  
  18.     NSLog(@"%@",filePath);  
  19.     NSString *file = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];  
  20.     NSLog(@"%@",file);  
  21.     return YES;  
运行结果:

[plain]  view plain copy
  1. /Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4  
  2. /Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4/Documents  
  3. /var/folders/fd/61_st6k93vbcnn7z7kkmcdh40000gn/T/  
  4. /Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4/HomeDictionaryTest.app  
  5. /Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4/HomeDictionaryTest.app  
  6. /Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4/HomeDictionaryTest.app/HomeDictionaryTest  
  7. Users/rjxyrjxy/Library/Application Support/iPhone Simulator/5.1/Applications/66504B82-7E7F-4FE4-8871-81348A4D0DD4/HomeDictionaryTest.app/Chen_Yilong.rtf  
  8.  {\rtf1\ansi\ansicpg936\cocoartf1138\cocoasubrtf470  
  9. {\fonttbl}  
  10. {\colortbl;\red255\green255\blue255;}  
  11. \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0  
  12. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值