alina
alina

Reputation: 107

How to use data in socket.io ? in swift

self.socket.on("opponentFound") { (data , acc) in
    print(data)
 }

and this is data from the top socket :

[{
ads =     {
    link = "https://www.digikala.com/mag/wp-content/uploads/2017/12/Info-Yalda-PR-4.gif";
    timer = 10;
    type = gif;
};
sId = "vKSm6RyR82xpi-8WAAAl";
username = "Player-4065";
word = "somthing"; }]

i want to print "link" and "word"

How should I do this ?

Upvotes: 0

Views: 888

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can try

do { 
      let dat = try JSONSerialization.data(withJSONObject:data)
      let res = try JSONDecoder().decode([Root].self,from:dat)
  }
  catch {
        print(error)
  }

struct Root: Codable {
    let ads: Ads
    let sId, username, word: String 
}

struct Ads: Codable {
    let link,type: String
    let timer: Int 
}

Upvotes: 2

Related Questions