swift 数组和字典保存到文件中
发布时间:2020-12-14 07:00:27 所属栏目:百科 来源:网络整理
导读:1.保存数组到文件中 //MARK:1,数组(Array)的存储和读取 func testSaveArrayPlist() { let arry = NSArray(objects: "stev","baidu.com","com","12344","robinson") let filePath:String = NSHomeDirectory() + "/Documents/tf.plist" arry.writeToFile(fi
1.保存数组到文件中 //MARK:1,数组(Array)的存储和读取 func testSaveArrayPlist() { let arry = NSArray(objects: "stev","baidu.com","com","12344","robinson") let filePath:String = NSHomeDirectory() + "/Documents/tf.plist" arry.writeToFile(filePath,atomically: true) tfArray = NSArray(contentsOfFile:NSHomeDirectory() + "/Documents/tf.plist") print(tfArray) }
Optional(<__NSCFArray 0x7dbba000>( stev,baidu.com,com,12344,robinson ) ) 2.保存字典到文件中 func testSaveDicPlist(){ let myArray = [ [ ["name":"小明","url":"google.com"],["name":"张三","url":"tf234.com"],["name":"里斯","url":"csdn.com"] ],[ ["name":"张三","url":"csdn.com"] ] ] let filePath:String = NSHomeDirectory() + "/Documents/tfDic.plist" NSArray(array: myArray).writeToFile(filePath,atomically: true) print(filePath) tfArray = NSArray(contentsOfFile:NSHomeDirectory() + "/Documents/tfDic.plist") print(tfArray) } 字典打印结果如下: Optional(<__NSCFArray 0x7aa6f4a0>( <__NSCFArray 0x7aa6df50>( { name = "U822aU54e5"; url = "hangge.com"; },{ name = "U767eU5ea6"; url = "baidu.com"; },{ name = google; url = google; } ),<__NSCFArray 0x7aa70de0>( { name = 163; url = "163.com"; },{ name = google; url = "google.com"; } ) ) ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |