分类 笔记 下的文章

沙盒文件读写

//  ViewController.swift

import UIKit
//默认生成的主控制器类
class ViewController: UIViewController {
    //当控制器的视图类夹在完成时调用
    override func viewDidLoad() {
        super.viewDidLoad()
        self.testSandBoxFileWriterOperation()//调用写文件方法
        self.testSandBoxFileReadOperation()//调用读文件方法
        // Do any additional setup after loading the view, typically from a nib.
    }
    //系统触发内存警告的时候调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //写沙盒文件
    func testSandBoxFileWriterOperation()
    {
        //获取程序Home目录
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>
        let documentPath:String? = paths[0]
        //判断是否获取成功
        if let _ = documentPath
        {
            //拼接实际文件路径
            let filePath = documentPath! + "/MagixBox.txt"
            //测试存储内容
            let content:NSArray = ["Hello,world","hahahahah","中文"]
            //写入文件,写入的内容是xml格式的
            let bRet = content.writeToFile(filePath, atomically: true)
            //打印写入结果
            if bRet == true
            {
                print("成功")
            }
            else
            {
                print("失败")
            }
        }
    }
    //读取沙盒文件内容
    func testSandBoxFileReadOperation()
    {
        //获得程序Home路径
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>
        let documentPath:String? = paths[0]
        //判断是否获取成功
        if let _ = documentPath
        {
            //拼接文件路径
            let filePath = documentPath! + "/MagixBox.txt"
            //读取内容,这里的到的内容是一个可选类型
            let fileContent = NSArray(contentsOfFile: filePath)
            print(fileContent!)
        }

    }

}

查询沙盒路径

//
//  ViewController.swift
//  持久化数据-ns
//
//  Created by admin on 16/1/19.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 应用程序根目录
        let home = NSHomeDirectory()
        // 保存应用需要持久化,并且需要在不同设备间同步的数据,itunus会备份这个目录
        let document = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 获得libray目录路径
        let library = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 缓存文件路径,itunus不会备份此目录,存放需要持久化,但是不需要在不同设备间同步的数据
        let caches = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 临时文件路径
        let temp = NSTemporaryDirectory()
        print(home)
    }
}

ViewController文件

记录下

//  ViewController.swift

import UIKit
//默认生成的主控制器类
class ViewController: UIViewController {
    //当控制器的视图类夹在完成时调用
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    //系统触发内存警告的时候调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //


}

AppDelegate.swift文件

记录下

//  AppDelegate.swift
//导入UIKit库
import UIKit
//当前app的入口点
@UIApplicationMain
//当前UIApplication的委托类,继承自UIResponder(响应用户的交互行为),实现协议UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate {
    //当前程序的窗口
    var window: UIWindow?

    //当前App夹在成功,调用这个
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
    //app在从当前激活状态变成不激活状态,调用这个方法,例如在app运行过程中接到电话
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    //app进入到后台,调用这个方法,例如在app使用中按 home 键
    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    //从后台将要转入前台的时候调用,比如双击 home 键,单击 app 图标
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    //本来在后台,现在即将转向前台,并且即将成为激活状态时调用
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    //程序退出的时候调用
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

swift使用第三方开源库

参考网址 文章网址
我使用的Workspace的方式引入的
1.创建Workspace,名称随意,我这里的是 DemoAppWorkespace
在xcode中创建即可
2.在workespace中创建自己的项目
DemoAppWorkspace中创建项目DemoApp
3.下载第三方库,这里以SwiftyJSON库为例
用git下载即可
git clone https://github.com/SwiftyJSON/SwiftyJSON.git
4.引入第三方库
在文件夹中打开下载下来的文件,会看到以.xcodeproj结尾的文件,比如这个库的就是SwiftyJSON.xcodeproj,直接拖到DemoAppWorkespace中即可
5.为App添加依赖
DemoAppGenral选项卡中,添加Linked Frameworks and Libraries,在弹出的对话框中选择要引入的库,我这里是SwiftyJSON的ios版本
6.添加头文件
DemoApp中添加头文件,并引入

#import <SwiftyJSON/SwiftyJSON.h>