模型

1 将数据存储到硬盘,将硬盘上的数据在读回内存

2 文件存储:

NSFileHandle 对文件的读写

NSData 二进制数据

NSString 表示文件路径

NSFileManager(对文件的操作创建、删除、改名、是不是文件夹)

【day0101_NSString】:NSString读取存储用法

NSString 路径用于表达文件的位置(/User/Apple/yz/docu...)

相对路径 apple/

绝对路径 /User/Apple

- (void)viewDidLoad

{

[superviewDidLoad];

NSString *path = @"/Users/tarena/yz/day01";

NSString *path2 = [path stringByAppendingPathComponent:@"use"]; // 生成新的字符串

NSLog(@"%@",[path stringByAppendingPathComponent:@"use"]);

// 拆分一个路径的component

NSArray *components = [path pathComponents];

[components enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

NSLog(@"%@",obj);

}];

// /User/apple/documents/ios core

// -> day20/nsstring/nsstring/main.m

NSString *path3 = @"/User/apple/documents/ios core";

NSString *path4 = @"day20/nsstring/nsstring/main.m";

NSString *newPath = [path3 stringByAppendingPathComponent:path4];

NSLog(@"%@",newPath);

// 追加文件后缀

path = [path stringByAppendingPathComponent:@"111"];

path = [path stringByAppendingPathExtension:@"jpg"]; // 补.

NSLog(@"%@",path);

// 取出文件的扩展名

path = @"/user/yz/a.jpg";

path = [path pathExtension];

NSLog(@"%@",path);

// 单词首字母大写

path = @"i love obc";

path = [path capitalizedString];

NSLog(@"%@",path);

path = [path uppercaseString]; // 全部大写

path = [path lowercaseString]; // 全部小写

NSLog(@"%@",path);

int i = [path hash];

NSLog(@"%d",i);

// 获取文件名或文件夹名

path = @"/use/sdg/gg.jpg";

NSLog(@"%@",[path lastPathComponent]);

// 获取父目录

path = @"/a/b/c/d.jpg";

NSLog(@"%@",[path stringByDeletingLastPathComponent]);

NSLog(@"%@",[path stringByDeletingPathExtension]);// 去除扩展名 /a/b/c/d

}

【day0102_NSFileManager】:文件管理器

NSFileManager(对文件的操作创建、删除、改名、是不是文件夹)

fileExistsAtPath 检测文件是否存在并判断是否是文件夹

createDirectoryAtPath 参数YES表示如果创建文件夹时如果中间没有那个文件夹会自动创建

- (void)viewDidLoad

{

[superviewDidLoad];

self.fileManager = [NSFileManagerdefaultManager];

// 检查文件是否存在

NSString *path = @"/Users/tarena/yz/第一阶段(OC-Fd)/Foundation";

if ([self.fileManagerfileExistsAtPath:path]) {

NSLog(@"文件存在");

}

BOOL isDirectory = NO;

if ([self.fileManagerfileExistsAtPath:path isDirectory:&isDirectory]) {

if (isDirectory) {

NSLog(@"是文件夹");

}

}

// 创建文件夹

NSString *createDirectory = @"/Users/tarena/Desktop";

// 参数YES表示如果创建文件夹时如果中间没有那个文件夹会自动创建

[self.fileManagercreateDirectoryAtPath:[createDirectory stringByAppendingPathComponent:@"myDirectory/dir/dir/dir"] withIntermediateDirectories:YESattributes:Nilerror:Nil]; // 如果用NO那么创建文件夹的父文件必须存在才能创建

// 练习

// /Users/tarena/yz/第三阶段(UI核心_Model赵哲)/day01

// QQ/Users

//   10086

//     images

//     cache

//     download

//     history

//   10010

//     images

//     cache

//     download

//     history

//   10000

//     images

//     cache

//     download

//     history

NSString *rootPath = @"/Users/tarena/yz/第三阶段(UI核心_Model赵哲)/day01";

NSString *usersDirectory = @"QQ/Users";

NSArray *users = @[@"10086",@"10010",@"10000"];

NSArray *directorys = @[@"images",@"cache",@"download",@"history"];

for (NSString *user in users) {

for (NSString *directory in directorys) {

[self.fileManagercreateDirectoryAtPath:[rootPath stringByAppendingPathComponent:[usersDirectory stringByAppendingPathComponent:[user stringByAppendingPathComponent:directory]]] withIntermediateDirectories:YESattributes:Nilerror:Nil];

}

}

NSString *path2 = @"/Users/tarena/Desktop";

NSArray *directoryArray = [self.fileManagercontentsOfDirectoryAtPath:path2 error:Nil];

NSLog(@"%@",directoryArray);

// 遍历文件夹下所有的子文件夹

for (NSString *subDirectory in directoryArray) {

NSString *directoryPath = [path2 stringByAppendingPathComponent:subDirectory];

BOOL isDirectory = NO;

if ([self.fileManagerfileExistsAtPath:directoryPath isDirectory:&isDirectory] && isDirectory) {

NSLog(@"子文件夹名:%@",subDirectory);

}

}

}

【day0103_Sandbox】:Sandbox沙箱

NSHomeDirectory()  获取app根路径

[[NSBundle mainBundle] resourcePath] 获取沙箱根路径

// 获取app根路径

NSString *homePath = NSHomeDirectory();

NSLog(@"%@",homePath);

// 获取沙箱根路径

NSString *bundle = [[NSBundlemainBundle] resourcePath];

NSLog(@"%@",bundle);

// 使用fileManagager获取documents路径

NSFileManager *fileManager = [NSFileManagerdefaultManager];

NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask];

NSURL *url = urls[0];

NSString *documentsPath = [url path]; // 发送path消息将url转为字符串

NSLog(@"%@",documentsPath);

// 获取缓存目录

NSURL *url2 = [fileManager URLsForDirectory:NSCachesDirectoryinDomains:NSUserDomainMask][0];

NSLog(@"%@",[url2 path]);

// 获取tmp目录

NSString *tmpPath = NSTemporaryDirectory();

NSLog(@"%@",tmpPath);

【day0104_Note便签】

保存和读取

- (IBAction)barButtonReadNote:(id)sender {

NSString *notePath = [NSHomeDirectory() stringByAppendingPathComponent:@"note.txt"];

NSString *stringNote = [NSStringstringWithContentsOfFile:notePath encoding:NSUTF8StringEncodingerror:Nil];

self.textViewNote.text = stringNote;

}

- (IBAction)barButtonSaveNote:(id)sender {

NSString *stringNote = self.textViewNote.text;

NSString *notePath = [NSHomeDirectory() stringByAppendingPathComponent:@"note.txt"];

[stringNote writeToFile:notePath atomically:YESencoding:NSUTF8StringEncodingerror:Nil];

self.textViewNote.text = @"";

}

【day0105_StringAndObject】:字符串和对象之间的转换

/*

作业

0.    复习

1.    TMessage 保存

读取

Documents/messages.txt

1|TXT|今天真高兴

0|TXT|那真是太好了

1|TXT|你在干什么?

0|TXT|我正在帮助全世界的人过上更好的生活

1|TXT|这么好

0|TXT|谢谢你的夸奖,我都害羞了,呵呵

文件 -> 字符串 -> 数组(字符串) -> 数组(对象) -> TRMessageViewController

存储*

点击发送的时候

1)    数组增加对象

2)    tableview插入动画

insertRowsAtIndexPathes:withRowAnimation:

3)    把数组的对象翻译成换行的字符串

4)  写入文件

*/

- (void)viewDidLoad

{

[superviewDidLoad];

NSString *home = NSHomeDirectory();

NSLog(@"%@",home);

// 文件 -> 字符串

NSFileManager *fileManager = [NSFileManagerdefaultManager];

NSString *documentPath = [[fileManager URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask][0] path];

NSLog(@"%@",documentPath);

NSString *stringFromFile = [NSStringstringWithContentsOfFile:[documentPath stringByAppendingPathComponent:@"messages.txt"] encoding:NSUTF8StringEncodingerror:Nil];

NSLog(@"%@",stringFromFile);

// 字符串 -> 数组(字符串)

NSArray *arrayFromString = [stringFromFile componentsSeparatedByString:@"\n"];

NSLog(@"%@",arrayFromString);

// 数组(字符串) -> 数组(对象)

for (NSString *string in arrayFromString) {

NSArray *messageFromString = [string componentsSeparatedByString:@"|"];

MXMessagesViewController *messageViewController = [[MXMessagesViewControlleralloc] init];

messageViewController.message = messageFromString[2];

messageViewController.state = messageFromString[0];

}

}

01-IOSCore - NSString、NSFileManager、NSBundle、StringAndObjectConvert的更多相关文章

  1. NSData和NSString 、 NSFileManager

    1 NSData和NSMutableData的基本使用 1.1 问题 NSData类是IOS提供的用于以二进制的形式操作文件数据的类,NSData有两个常用的属性length和bytes,length ...

  2. NSData、NSString 、 NSFileManager

      1 NSData和NSMutableData的基本使用 1.1 问题 NSData类是IOS提供的用于以二进制的形式操作文件数据的类,NSData有两个常用的属性length和bytes,leng ...

  3. CSS.01 -- 选择器及相关的属性文本、文字、字体、颜色、

    与html相比,Css支持更丰富的文档外观,Css可以为任何元素的文本和背景设置颜色:允许在任何元素外围设置边框:允许改变文本的大小,装饰(如下划线),间隔,甚至可以确定是否显示文本. 什么是CSS? ...

  4. Hinge Loss、交叉熵损失、平方损失、指数损失、对数损失、0-1损失、绝对值损失

    损失函数(Loss function)是用来估量你模型的预测值 f(x) 与真实值 Y 的不一致程度,它是一个非负实值函数,通常用 L(Y,f(x)) 来表示.损失函数越小,模型的鲁棒性就越好. 损失 ...

  5. swift 基础小结01 --delegate、Optional、GCD的使用、request请求、网络加载图片并保存到沙箱、闭包以及桥接

    本文主要记录swift中delegate的使用.“?!”Optional的概念.GCD的使用.request请求.网络加载图片并保存到沙箱.闭包以及桥接. 一.delegate的使用 swift中de ...

  6. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  7. 反射01 Class类的使用、动态加载类、类类型说明、获取类的信息

    0 Java反射机制 反射(Reflection)是 Java 的高级特性之一,是框架实现的基础. 0.1 定义 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...

  8. 数据库01 /Mysql初识、基本指令、数据库密码相关、创建用户及授权

    数据库01 /Mysql初识.基本指令.数据库密码相关.创建用户及授权 目录 数据库01 /Mysql初识.基本指令.数据库密码相关.创建用户及授权 1. 数据库概述 2. 数据库管理系统/DBMS ...

  9. python网络编程01 /C/S架构|B/S架构、网络通信原理、五层协议、七层协议简述、端口映射技术

    python网络编程01 /C/S架构|B/S架构.网络通信原理.五层协议.七层协议简述.端口映射技术 目录 python网络编程01 /C/S架构|B/S架构.网络通信原理.五层协议.七层协议简述. ...

  10. C#基础知识01(continue、break 和 return、ref 和 out)

    break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...

随机推荐

  1. ORACLE 使用RMAN管理归档日志 archived log

    oracle 归档日志通常使用rman进行管理,作为备份集的一部分 正常情况下,可以根据方法删除 1 过期策略 crosscheck archivelog all; delete expired ar ...

  2. FZU Problem 2169 shadow

    http://acm.fzu.edu.cn/problem.php?pid=2169 题目大意: S王国有N个城市,有N-1条道路.王都为编号1的城市.叛军驻扎在很多城市.除了王都外有K个城市有军队, ...

  3. windows 2008 远程端口3389修改小记

    修改远程端口使服务器更加安全,win2008上大致与win2003的配置差不多,有些细微的差别,在此小记一下. 简要步骤: 1.打开远程连接功能(默认都是已经打开的) :开始>计算机>属性 ...

  4. 前端笔试面试中的常用知识点总结(CSS)

    1.CSS选择器的优先级!important  > 内联 > id选择器 > 类选择器 > 标签选择器多个类选择器叠加(256)之后的优先级大于一个id选择器!importan ...

  5. 移除GridView中的重复项

    1. The HTML Markup <div> <asp:GridView ID="GridView1" runat="server" Au ...

  6. 安卓入门学习之Hello,world!

    第二章 Hello,world! 本文同时发表至简书,不为什么,用他的MarkDown在线编辑爽得要哭. 注意:本章节所使用的开发工具为Eclipse 以Android编程权威指南作为学习书物 第一节 ...

  7. mina教程

    关于mina介绍这里不做阐述..... 我们先做一个关于mina的helloworld 首先先下载mina包:http://mina.apache.org/ (如果你已经下载,此步骤忽略) 下载下来以 ...

  8. java和C#之间SOCKET通信的问题

    转自:http://www.cdtarena.com/javapx/201307/9170.html java和C#之间SOCKET通信的问题 一.服务器端(使用java编写) /** * 监听客户端 ...

  9. VPN各种方案

    http://www.maimiaovpn.com 大家都是用什么方法FQ的?我原来用的XskyWalker浏览器 但我用的电信网现在不行了.大家用的什么方法可否告诉我一下我用yes-vpn,10元一 ...

  10. Xamarin.Android开发实践(三)

    原文:Xamarin.Android开发实践(三) 一.前言 用过Android手机的人一定会发现一种现象,当你把一个应用置于后台后,一段时间之后在打开就会发现应用重新打开了,但是之前的相关的数据却没 ...