Swift2.0系列]Error Handling(项目应用篇)
来源:互联网 发布:富士康mac是什么意思啊 编辑:程序博客网 时间:2024/05/18 00:12
1.FileManager中的应用
倘若你只是想看FileManager中的 Error Handling是如何实现的,请找到3.删除文件以及4.获取文件信息。我分别为你提供了
do-catch
以及try?
的使用方法。
打开Xcode
,选中Single View Application
,输入项目名称例如FileManagerDemo,点击Next,选择文件放置路径。此时你已经建立了一个新的工程。
选中ViewController.swift
文件,找到viewDidload()
文件,写入如下的代码。
override func viewDidLoad() { super.viewDidLoad() // 1.获取路径操作代码 // 沙盒下有三个目录 分别是Documents/Library/tmp 通常我们将文件放到Documents下 // 获取document的文件夹路径 var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let pathDocuments = path[0] print(pathDocuments) // 2. 创建文件操作 let file1 = "file1.txt" let file2 = "file2.txt" let file1Path: String = NSString(format: "%@/%@", pathDocuments,file1) as String//生成一个绝对路径 let file2Path: String = NSString(format: "%@/%@", pathDocuments,file2) as String//生成一个绝对路径 // 进行file1文件生成操作 if NSFileManager.defaultManager().fileExistsAtPath(file1Path as String){ print("文件已存在") }else{ let data = "Hello world By Swift2.0 file1".dataUsingEncoding(NSUTF8StringEncoding) NSFileManager.defaultManager().createFileAtPath(file1Path, contents: data, attributes: nil)//创建新文件 //当然也有直接向已存在文件中写入内容的方法 } // 进行file2文件生成操作 if NSFileManager.defaultManager().fileExistsAtPath(file2Path as String){ print("文件已存在") }else{ let data = "Hello world By Swift2.0 file2".dataUsingEncoding(NSUTF8StringEncoding) NSFileManager.defaultManager().createFileAtPath(file2Path, contents: data, attributes: nil)//创建新文件 //当然也有直接向已存在文件中写入内容的方法 } // 3.删除文件 将原先的创建的删除 let deletePath = NSString(format: "%@/%@", pathDocuments,file1) as String print("要删除的文件路径:\(deletePath)") do{ try NSFileManager.defaultManager().removeItemAtPath(deletePath) }catch let error as NSError{ print("error is \(error)") } // 以下内容只做拓展 /* var arrayContent = NSArray(contentsOfFile: filePath)//必须是完整路径 var dictContent = NSDictionary(contentsOfFile: filePath) var imgContent = UIImage(contentsOfFile: filePath) */ // 4.获取文件信息 // 文件属性 是一个字典类型 let attrFile1 = try? NSFileManager.defaultManager().attributesOfItemAtPath(file1Path) let attrFile2 = try? NSFileManager.defaultManager().attributesOfItemAtPath(file2Path) print(attrFile1)//打印nil 因为file1不存在 print(attrFile2)//打印出信息 }
文/PPPPPPMST(简书作者)
原文链接:http://www.jianshu.com/p/8e1fe654bdec
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文链接:http://www.jianshu.com/p/8e1fe654bdec
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
0 0
- Swift2.0系列]Error Handling(项目应用篇)
- WF4.0 入门系列(七)Error Handling
- swift2.1_Language Guide_Error Handling
- Error Handling
- Error Handling
- Swift3.0学习笔记-Error Handling
- swift2.0
- Requirements of error handling
- 2.4 Handling &Error Code
- Error handling in WCF
- 错误处理(Error Handling)
- Error Handling In VBA
- Error handling in VBScript
- RadInput Error handling
- 【pig】Error Handling
- Erlang Error Handling
- Error Handling with exceptions
- Error Handling and Debugging
- mysql 安装
- 常用查找和排序
- 解决 MinGw编译wxWidgets出错
- 理解Python的With语句
- Android studio配置NDK问题小结
- Swift2.0系列]Error Handling(项目应用篇)
- BRISK
- Android引导用户打开应用市场评分自己的app
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- Android创建文件夹及文件并写入数据
- java征途的点滴
- Linux ln命令 - 建立文件/目录链接(只看红色部分)
- Xcode如何手动添加pch文件
- android/java 中两种延时执行的方法