1. Dictionary to Data
let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample)
2. Data to Dictionary
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any]
3. String to Data
var somedata = testString.data(using: String.Encoding.utf8)
4. Data to String
var backToString = String(data: somedata!, encoding: String.Encoding.utf8) as String!
5. JsonString to Data
let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
6. jsonData to Dictionary
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
// here "decoded" is of type `Any`, decoded from JSON data
// you can now cast it with the right type
if let dictFromJSON = decoded as? [String:String] {
// use dictFromJSON
}
'iOS' 카테고리의 다른 글
[iOS] how to create iOS/swift .gitignore in MacOS terminal(console) (0) | 2019.02.18 |
---|---|
[swift3] Create Simple Progress Dialog View (ProgressBar, ProgressIndicator, LoadingProgress) (0) | 2017.06.27 |
[swift3] Handler Example (0) | 2017.06.21 |
[iOS]Pods framework not found ... error (0) | 2017.05.22 |
[iOS]페이스북 연동오류 - 오류가 발생했습니다. 잠시 후 다시 시도하세요. _ Enable sendbox to facebook sharing on iOS (0) | 2015.03.31 |