Convert Swift Dictionary to String

Related searches

For testing and debugging I am trying to put the content of Dictionary to a String. But have no clue hows it going to achieve. Is it possible? If yes, how.

Dictionary is fetched from web service so I have no idea the key values it have. I want to use the data in app.

In Objective C %@ was enough to store anything in NSString.

Just use the description property of CustomStringConvertible as

Note: Prior to Swift 3 (or perhaps before), CustomStringConvertible was known as Printable.

Convert Dictionary to JSON in Swift, data( withJSONObject: dictionary, options: []) { let theJSONText = String(data: theJSONData, encoding: . ascii) print("JSON string = \(theJSONText!)") } Or in Swift 2 syntax: import Foundation let dictionary = ["aKey": "aValue", "anotherKey": "anotherValue"] let theJSONData = NSJSONSerialization. It might happen that you need to convert a swift Set,Array,Dictionary to string. It might be because you want to log or might just want plain string. It is very easy to Convert Swift Collections to String by using description.

Dictionary to string with custom format:

let dic = ["key1":"value1", "key2":"value2"]

let cookieHeader = (dic.flatMap({ (key, value) -> String in
    return "\(key)=\(value)"
}) as Array).joined(separator: ";")

print(cookieHeader) // key2=value2;key1=value1

Crash : Convert dictionary to Json string in Swift 3, String is not of type AnyObject. Objects are reference types, but String in swift has value semantics. A String however, can be of type Any, so the code below� Python | Convert dictionary object into string Last Updated: 17-10-2019 Dictionary is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.

You can just print a dictionary directly without embedding it into a string:

let dict = ["foo": "bar", "answer": "42"]

// [foo: bar, answer: 42]

Or you can embed it in a string like this:

let dict = ["foo": "bar", "answer": "42"]

println("dict has \(dict.count) items: \(dict)")
  // dict has 2 items: [foo: bar, answer: 42]

Dictionary to JSON string and JSON string to dictionary , Updated for Swift 4 on 2017/09/06 - Because I always end up Googling The dictionary is converted to Data which contains an UTF8 encoded� Welcome to Swift Tutorial. In this tutorial, we will learn how to create a dictionary with (key, value) pairs in Swift programming. Dictionaries are used to store a collection of (key, value) pairs. In a dictionary, you can only insert (key, value) pairs whose key is unique in the dictionary. Dictionary can be created in two ways. They are: Create an empty Dictionary To create an empty

Jano's answer using Swift 5.1:

let dic = ["key1": "value1", "key2": "value2"]
let cookieHeader = { $0.0 + "=" + $0.1 }.joined(separator: ";")
print(cookieHeader) // key2=value2;key1=value1

How to create JSON from a dictiona…, So using Swift I did the following, When I print the jsonString, I get let jsonData = try messageDictionary, Converting to a string in order to convert it back to a stream of bytes seems unnecessary. There are plenty of APIs which just give you back a dictionary, or expect a dictionary, and it's nice to be able to interchange them easily with Swift structs or objects, without having to write endless boilerplate code.

swift convert dictionary to json Code Example, Get code examples like "swift convert dictionary to json" instantly right from your if let jsonString = String(data: jsonData, encoding: .utf8) {. 5. Warning: this is a convenience method to convert a JSON string to a dictionary if, for some reason, you have to work from a JSON string. But if you have the JSON data available, you should instead work with the data, without using a string at all. Swift 3

Several mistakes here. Why using Foundation's NSDictionary instead of Swift's Dictionary?! Also why returning a new dictionary with a String as value, instead of returning the actual JSON data? This doesn't make sense. Also the implicitly unwrapped optional returned as an optional is really not a good idea at all. – Eric Aya Dec 13 '18 at 9:23

The general case for creating an array out of ONLY VALUES of a dictionary in Swift 3 is (I assume it also works in older versions of swift): let arrayFromDic = Array({ $0 }) Example:

  • %@ on an object, just calls the description method. So you can just do myDict.description to get a String.
  • thats exactly I was looking for. println cannot be assigned to object - String in my case. (Y)
  • will it work if the value in dictionary is not string:string and is string:array.
  • "(key)=(value)" is calling description on your behalf so the result would be for instance key=["1", "2", "3"]. If you want something different you have to customize yourself.
  • talking about usage of flatMap
  • The element returned from flatMap is a string, disregarding what the key and value are, so yes, it will work with string:array.
  • @Jano Thanks a lot +1 , Worked like charm!!