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

1.保存数组到文件中@H_502_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)
    }


@H_502_1@ 数组打印结果如下:

Optional(<__NSCFArray 0x7dbba000>(
stev,baidu.com,com,12344,robinson
)
)


2.保存字典到文件中@H_502_1@

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 = "\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";
}
)

)
)

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...