0

I have spend a lot of time googling and I could not get a solution to work so I hope someone can help me.

I have a string called user which is of type: < hit>JSON. I can get information out of it by using:

user.object["displayname"] ?? ""

This works fine however it gives me the result between "". So if someones displayname is stackoverflowuser it will give me: "stackoverflowuser". Is there a way to get this as a string without the ""?

Edit:

I use the following API: https://www.algolia.com/doc/api-reference/api-methods/search/#examples.

This is my code to get the user object:

index.search(query: "\(searchtext)") { result in
   if case .success(let response) = result {
      for user in response.hits {
         let resultDisplayname = "\(user.object["displayname"] ?? "")"

      }
   }
 }

The resultDisplayname is an example of a variable that I want to turn into a String.

9
  • Is there something wrong with, for example, "chrispsv"? Cause it's swift syntax. Check this documentation: developer.apple.com/documentation/swift/string#2919514 Commented May 4, 2022 at 8:45
  • Well the problem is that if I want to show someones username it is between "". And that just doesn't look good. Also if I want to show someones profilepicture. The link to the url is also between "". This way the url doesn't work and I can't show the picture. Commented May 4, 2022 at 9:26
  • So what I can do is remove the first and last character of the string (because those are the "") but that is an ugly solution. Commented May 4, 2022 at 9:41
  • What do you mean show someones username? If you got the string from displayname and set to label, like, nameLabel.text = user.object["displayname"] ?? "". It will work perfectly. Commented May 4, 2022 at 9:52
  • If I add .text behind it I get the following error in xcode: "Value of type 'JSON' has no member 'text'" Commented May 4, 2022 at 10:10

1 Answer 1

1

let's assume your JSON is like this format

{
    "displayname": "chrispv",
    "age": 30
}

Create a swift object to represent it

struct Person: Decodable {
    let displayname: String
    let age: Int
}

Then execute your search query using object above

index.search(query: "\(searchtext)") { result in
    if case .success(let response) = result {
        do {
            let persons: [Person] = try response.extractHits()
            for person in persons {
                print(person.displayname)
            }
        } catch let error {
            print("Decoding error")
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I understand this is the best solution. However it is very hard to create a struct of the query result since the user returns a very complicated JSON format. Is there a way to create a struct that is flexible?
I think it's better if the data model mapping 1-1 with JSON structure. However, you can just take what you really need. In this scenario, remove age and keep displayname it's ok. Make sense?
Thank you very much! This fixed my issue!! I did not know that it was possible to decode JSON in a struct that wasn't mapped 1-1!
It's no big deal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.