-1

I have this class:

class User {

    let name: String
    let email: String
    let password: String
    let provider: String
    let uid: String

    init (name: String, email:String, password: String, provider: String, uid: String) {

        self.name = name
        self.email = email
        self.password = password
        self.provider = provider
        self.uid = uid
    }
    var dictionary: [String: [String: Any]] {
        return ["user" : ["name": name, "email": email, "password": password, "provider": provider, "uid": uid]
        ]
    }
    var nsDictionary: NSDictionary {

        return dictionary as NSDictionary
    }
}

When I create the user:

let userNew = User(name: userName!, email: userEmail!, password: "", provider: provider, uid: userId!)

My user is created like this:

Optional({
    user =     {
        email = "jgurban@gmail.com";
        name = "Juan Gil";
        password = "";
        provider = Facebook;
        uid = 1925035627521626;
    };
})

But I need all values with ""

Optional({
user =     {
    email = "jgurban@gmail.com";
    name = "Juan Gil";
    password = "";
    provider = "Facebook";
    uid = "1925035627521626";
};

})

How can I get all values with quotation marks?

Thanks in advance!

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
Juan Gil
  • 21
  • 1
  • 4
  • This looks like debugger output. The quotes are not part of the actual data in memory. What exactly are you trying to do with this output? – BergQuester Dec 15 '17 at 21:13
  • Are you using Swift4? If you are trying to encode your object into a JSON String, you should make it conform to `Encodable` protocol and use `JSONEncoder` `encode` method. If you are using an older Swift version (Xcode 8.x or earlier) you need to use `JSONSerialization` method `data(withJSONObject:)` – Leo Dabus Dec 15 '17 at 21:40
  • I am using Swift 4. I have to make a POST request to register a user with this API: All my be a String. user[email] user[name] user[provider] user[uid] – Juan Gil Dec 15 '17 at 21:45
  • What is your Xcode version? – Leo Dabus Dec 15 '17 at 21:46
  • Xcode 9.1 (9B55) – Juan Gil Dec 15 '17 at 21:52
  • I use SwiftyJSON to get the response from Facebook and create the user: let userName:String? = json["name"].stringValue let userEmail:String? = json["email"].stringValue let userId: String? = json["id"].stringValue let provider = "Facebook" let userNew = User(name: userName!, email: userEmail!, password: "", provider: provider, uid: userId!) – Juan Gil Dec 15 '17 at 21:54

2 Answers2

0

To encode your object into JSON data, you should make your object conform to Encodable protocol and use JSONEncoder encode method to convert your object to JSON data. Note that you could use a class also but a structure is preferable:

struct User: Codable {
    let name: String
    let email: String
    let password: String
    let provider: String
    let uid: String
}

Sometimes when posting JSON data the API may require your object keys to be sorted. In order to achieve that you need to set the encoder outputFormatting property to .sortedKeys:

let jsonObject =  ["user" : User(name: "Juan Gil", email: "jgurban@gmail.com", password: "", provider: "Facebook", uid: "1925035627521626")]
do {
    let encoder = JSONEncoder()
    encoder.outputFormatting = .sortedKeys
    let jsondata = try encoder.encode(jsonObject)
    print(String(data: jsondata, encoding : .utf8)!)  // "{"user":{"email":"jgurban@gmail.com","name":"Juan Gil","password":"","provider":"Facebook","uid":"1925035627521626"}}\n"
} catch {
    print(error)
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thanks Leo, but I need to send an object User to the server with these values. Something like this:{ user = { email = "jgurban@me.com"; name = ""; password = 968knmsn; provider = ""; uid = ""; }; }. – Juan Gil Dec 15 '17 at 22:55
  • Yes. I suppose. The API url is: https://www.o-track.dk/api/v1/registration.json and it needs this format: user must be a Hash. Properties email, name, provider and uid must be String. The server respond me code 422. Thanks – Juan Gil Dec 15 '17 at 23:05
  • 422 suggests that the request was well-formed, but it was unable to process it for some reason. Maybe email address exists already? Maybe password failed some validation rules they have? It's hard to say. – Rob Dec 16 '17 at 01:03
  • @JuanGil You may need to sort your object keys. Check the updated answer. – Leo Dabus Dec 16 '17 at 02:09
  • Thanks! You were right Leo and thanks everybody for your help and suggestions. – Juan Gil Jan 13 '18 at 10:05
0

Your inquiry about double quotes in that "user = { ... }" output suggests that you might be conflating JSON with the output when you print the description dictionary. They're two completely different things.

If you're trying to build JSON in Swift 4 you can use Encodable with JSONEncoder:

struct User: Encodable {
    let name: String
    let email: String
    let password: String
    let provider: String
    let uid: String
}

And then you can do:

let user = User(name: "Juan Gil", email: "jgurban@gmail.com", password: "", provider: "Facebook", uid: "1925035627521626")
let data = try! JSONEncoder().encode(["user": user])

If you want to examine the result:

let string = String(data: data, encoding: .utf8)!
print(string)

It results in:

{"user":{"email":"jgurban@gmail.com","password":"","provider":"Facebook","uid":"1925035627521626","name":"Juan Gil"}}
Rob
  • 415,655
  • 72
  • 787
  • 1,044