I'm working with Xcode6-Beta6 and want to display an item from an NSArray on the screen.
extract from code is as follows
var task: AnyObject? = categoriesForOption.valueForKey("task")!
var taskName: UILabel = view.viewWithTag(10) as UILabel
taskName.text = task as? String
task contains ( Tea ) taskName contains nil
I've searched for solutions and found many answers, but none of them seem to work. The extract above is the nearest I've come to solving my problem. Hope someone can help me as I'm banging my head against the wall.
You could try:
var task: String! = String(categoriesForOption.valueForKey("task"))
taskName.text = task
I guess you meant NSDictionary
and not NSArray
.
I think this way it's more explicit:
var task = categoriesForOption.valueForKey("task") as? String
Type inferral will make task a String?
- it will be populated with a value if the value corresponding to the "task" key exists in the dictionary and if it's a string. It's nil
otherwise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.