2015/02/12

Swift IOS write a file


Code:
//
//  ViewController.swift
//  Sample
//
//  Created by C.Y.Fang on 2/12/15.
//  Copyright (c) 2015 C.Y.Fang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {


    let fileName = "test.cy"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let directory = NSSearchPathForDirectoriesInDomains(
            .AllLibrariesDirectory, .UserDomainMask , true)[0] as String
        let path = "\(directory)/\(fileName)"
        
        NSData().writeToFile(path, atomically: true)
        
        if(NSFileManager.defaultManager().fileExistsAtPath(path)){
            println("yes")
        }else{
            println("no")
        }
    }
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


執行結果: