iOS开发 - txt 文件转 plist

txt 文件转成 plist 文件
struct TxtToPlist {
    
    init() {
        let txtFilePath = NSBundle.mainBundle().pathForResource("22", ofType: "txt")
        //let gbk = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingASCII)
        let str = try! String(contentsOfFile: txtFilePath!, encoding: NSUTF8StringEncoding)
        print(str)  \\看看打印出来是什么东西在中间 再根据情况分割 一般是 \r \n
        let strArr = str.split("\n") as NSArray 
        print(strArr)
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
        let filePath = path.stringByAppendingString("/data666.plist")
        strArr.writeToFile(filePath, atomically: true)
        print(filePath) //取件地址
    }
}

extension String {

func split(str: String) -> [String] {
    if str.isEmpty {
        var strArr = [String]()
        for char in self.characters {
            strArr.append(String(char))
        }
        return strArr //空的话、无缝分割
    }
    return self.componentsSeparatedByString(str)
}
iOS开发 - txt 文件转 plist_第1张图片
TxtToPlist

你可能感兴趣的:(iOS开发 - txt 文件转 plist)