【问题标题】:Swift: cannot convert value of type <> to expected argument type <> when setting up a selectorSwift:设置选择器时无法将 <> 类型的值转换为预期的参数类型 <>
【发布时间】:2017-02-14 00:47:22
【问题描述】:

我收到此错误:

无法将 nsdictionary 类型的值转换为预期的参数类型字符串


这是我的代码:

let json:NSDictionary = try JSONSerialization.jsonObject(with: dataResponse, options: []) as! NSDictionary
self.performSelector(onMainThread: Selector(updateUIwithJsonResponse), with: json, waitUntilDone: true)

performSelector 正在调用这个函数:

func updateUIwithJsonResponse(json:NSDictionary) {
print(json)
}

你们中的任何人都知道我为什么会收到这个错误吗?

【问题讨论】:

  • 1.你为什么使用 NSDictionary 而不是 Swift 字典? 2、你为什么用performSelector而不是DispatchQueue
  • @rmaddy,因为我不需要排队。为什么你认为使用 DispatchQueue 会更好?
  • 使用DispatchQueue.main.async 更简单、更干净。您可以简单地在块内编写实际代码。无需弄乱选择器,它适用于任意数量和类型的参数。

标签: swift xcode nsdictionary


【解决方案1】:

你的方法有一个参数,所以试试

self.performSelector(onMainThread: #selector(updateUIwithJsonResponse(json:)), with: json, waitUntilDone: true)

【讨论】:

  • 为什么不能删除 json: in #selector(updateUIwithJsonResponse(json:)) ?
猜你喜欢
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多