Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

convert dictionary to data

let dic = ["2": "B", "1": "A", "3": "C"]

do {
    let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
    // here "jsonData" is the dictionary encoded in JSON data

    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
    }
} catch {
    print(error.localizedDescription)
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift isMemberOf 
Swift :: Named Associated Values Swift 
Swift :: how to read music library from iphone programmatically in swift 
Swift :: swift split an array into chunks 
Swift :: library not found for -lalan-sdk-react-native 
Swift :: redux trong swift 
Swift :: octobercms add extra slash to css url 
Swift :: swift 5 macos close app 
Swift :: get device height spritekit 
Swift :: while loop swift 
Swift :: circular array swift 
Ruby :: validates length rails 
Ruby :: rails skip authenticity token 
Ruby :: brew update ruby 
Ruby :: get current year in ruby 
Ruby :: rails routes grep 
Ruby :: ruby each with index 
Ruby :: add timezone in rails 
Ruby :: rails remove model 
Ruby :: rspec shared examples 
Ruby :: ruby timestamp 
Ruby :: ruby remove last element of string 
Ruby :: Convert Date and time to utc in rails 
Ruby :: How To Set Up Ruby on Rails with Postgres 
Ruby :: ruby delete first element of array 
Ruby :: ruby case statement multiple conditions 
Ruby :: .delete ruby 
Ruby :: rails localhost https 
Ruby :: ruby on rails sum nil 
Ruby :: SoC partial class 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =