美文网首页
将字符串,数组,字典 ,data

将字符串,数组,字典 ,data

作者: 伟伟机器人 | 来源:发表于2017-05-14 20:52 被阅读0次

    // //字符串
    // let name = "伟伟你好"
    // let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    // print(document[0])
    // //创建文件路径
    // let path = document[0] + "/index.txt"
    // //创建文件 文件内容
    // let fileM = FileManager()
    // fileM.createFile(atPath: path, contents: nil, attributes: nil)
    // print(path)
    // //写入文件
    // try! name.write(toFile: path, atomically: true, encoding: .utf8)
    // //移动文件 文件夹只能移文件夹 文件只能移文件
    // let tmp = NSTemporaryDirectory() + "index.txt"
    // try! fileM.moveItem(atPath: path, toPath: tmp)
    // //try! fileM.removeItem(atPath: tmp)
    //数组
    // let array = [1,2,3,2,4]
    // //创建路径
    // let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "arr.plist"
    // //创建文件
    // let file = FileManager()
    // file.createFile(atPath: document, contents: nil, attributes: nil)
    // let url = URL(fileURLWithPath: document)
    // (array as NSArray).write(to: url, atomically: true)
    // //读取
    // let arr1 = NSArray(contentsOfFile: document)
    // print(arr1!)
    //字典
    let dic = ["A":"Apple","B":"Bubble"]
    //创建路径
    let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "dic.plist"
    //创建文件
    let file = FileManager()
    file.createFile(atPath: document, contents: nil, attributes: nil)
    let url = URL(fileURLWithPath: document)
    (dic as NSDictionary).write(to: url, atomically: true)
    //读取
    let arr1 = NSDictionary(contentsOfFile: document)
    print(arr1!)

    相关文章

      网友评论

          本文标题:将字符串,数组,字典 ,data

          本文链接:https://www.haomeiwen.com/subject/lxxcxxtx.html