简体   繁体   English

Swift:如何在文件中保存不同类型的数据

[英]Swift: How to save data of different types in a file

I want to save data including string, number, date and coordinate in a file, and then transmit the file to a server. 我想将包括字符串,数字,日期和坐标的数据保存在文件中,然后将文件传输到服务器。 How to do this using swift? 如何使用swift做到这一点?

And I'd like to process these data from the server in the future. 而且我希望将来能处理来自服务器的这些数据。 What type of file is better to save them? 哪种类型的文件更适合保存?

If i am getting this right you can use NSData 如果我做对了,您可以使用NSData

First you have to create a dictionary like this 首先,您必须创建一个这样的字典

    var dictionary = [String:AnyObject]()
            dictionary["age"] = 13
            dictionary["name"] = "Mike"

Then you have to transform this dictionary into NSData using nsjsonserialization 然后您必须使用nsjsonserialization将此字典转换为NSData

if let data = NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error:nil) as NSData? {


                    request.HTTPBody = data

                }

But this is always depend on what the server is able to understand 但这始终取决于服务器能够理解的内容

Hope i helped. 希望我能帮上忙。 Sorry for my english 对不起我的英语不好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM