swift 数组和字典保存到文件中

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":"tf234.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 = "\U822a\U54e5";
    url = "hangge.com";
},
{
    name = "\U767e\U5ea6";
    url = "baidu.com";
},
{
    name = google;
    url = google;
}
)
,
<__NSCFArray 0x7aa70de0>(
{
    name = 163;
    url = "163.com";
},
{
    name = google;
    url = "google.com";
}
)

)
)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值