您的位置:首页 > 产品设计 > UI/UE

IOS_UI_本地存储

2015-09-09 17:04 489 查看
#import <UIKit/UIKit.h>

@interface AppDelegate :
UIResponder <UIApplicationDelegate>

@property (strong,
nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

#import "MainViewController.h"

@interface
AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc

{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor
whiteColor];

[self.window
makeKeyAndVisible];

[_window release];

MainViewController *mainVC = [[MainViewController
alloc]init];

self.window.rootViewController = mainVC;

[mainVC release];

return
YES;

}

#import <UIKit/UIKit.h>

@interface MainViewController :
UIViewController

@end

#import "MainViewController.h"

@interface
MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

//1. ios 系统的文件机制SandBox

NSLog(@"%@",NSHomeDirectory());

//user开头的是电脑地址

//一 : 沙盒机制 : 在文件内部可以自由读写,无权访问文件夹外部的内容

// 1. Documents : 用来存储用户相关的内容,比如 : 数据库文件.不要在这个文件夹保存体积大的文件, 如果开启了icloud服务,系统会把这个文件夹的所有内容上传到用户的云盘上

// 2. Caches(缓存) : 缓存文件夹.系统缓存文件和一些第三方的默认缓存地址都是这个文件夹.清除缓存就是清空缓存文件夹.

// 3. Preference : 给开发者配置应用程序使用.

// 4. tmp : 临时文件夹,存储临时文件.清除缓存时一起清除掉

// 二. 简单对象写入本地

NSString *str =
@"大芊芊在吃西瓜";

// 1. 把字符串写入本地要先写一个路径 拼接一个文件路径

//参数一 : 要找的系统文件夹(documents,caches)

//参数二 : 在什么系统区域搜索文件夹

//参数三 : YES绝对路径/user... NO相对路径~/documents... 1.txt 文件路径

//返回值只有一个路径放入数组中

NSString *docPath =[
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)lastObject];

NSString *strPath = [docPath
stringByAppendingString:@"/1.txt"];

// 2. 将字符串写入本地

// 参数一 : 要写入的路径

// 参数二 : YES保护线程安全 在写入过程是否保证线程安全

// 参数三 : 字符串的编码格式

// 参数四 : 错误

[str writeToFile:strPath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];

// 运行处地址 查找地址在文件中可以看到结果

//数组写入本地

NSArray *arr =
@[@"a",@"b",@"c"];

//数组拼接本地有两种形式xml或者plist

NSString *arrPath = [docPath
stringByAppendingPathComponent:@"2.xml"];

[arr writeToFile:arrPath
atomically:YES];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: