-1

here is what i do to parse int value:

anyObj = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! Dictionary<String,AnyObject>

let categoryId = String(anyObj["id"])


let link = "categoryId" + categoryId

print(link) 

where categoryId is integer value

here is what i got :

/category/Optional(127)

How to remove the word optional ?

NOTE: i have looked at this question. also i have tried this also let link = "categoryId" + categoryId! but without luck

thanks

Community
  • 1
  • 1
david
  • 3,310
  • 7
  • 36
  • 59

2 Answers2

2

A value for a key of a dictionary is always optional because the key might not exist.

You need to unwrap the optional

let categoryId = String(anyObj["id"]!)

A safer solution is optional binding

if let categoryId = anyObj["id"] {
   let link = "categoryId" + String(categoryId)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
-1
var link = "categoryId";
if let categoryId = String(anyObj["id"]) {
    link += categoryId
}
Pradeep K
  • 3,671
  • 1
  • 11
  • 15