【问题标题】:Cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int32>!'无法将“Int”类型的值转换为预期的参数类型“UnsafeMutablePointer<Int32>!”
【发布时间】:2018-05-03 08:45:16
【问题描述】:

通过使用这个Answer,我在 swift 4.1 中遇到了这个错误,即Cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer&lt;Int32&gt;!'

var notify_token: Int
notify_register_dispatch("com.apple.springboard.lockstate", notify_token, DispatchQueue.main, { (_ token: Int) -> Void in 
    var state: UInt64 = UINT64_MAX
    notify_get_state(token, state)
    if state == 0 {
        print("unlock device")
    }
    else {
        print("lock device")
    }

如何解决?

【问题讨论】:

  • var notify_token: Int32 ??检查这是否适合你 1
  • 是的,它的工作,但现在这个comming:Cannot convert value of type 'UInt64' to expected argument type 'UnsafeMutablePointer&lt;UInt64&gt;!'@Roshan

标签: ios swift darwin


【解决方案1】:

试试这样的:

var notify_token: Int32

notify_register_dispatch("com.apple.springboard.lockstate", &notify_token, DispatchQueue.main, { (_ token: Int) -> Void in
    var state: UInt64 = UINT64_MAX
    notify_get_state(token, state)
    if state == 0 {
        print("unlock device")
    }
    else {
        print("lock device")
    }
}

【讨论】:

  • 现在Cannot convert value of type '(Int) -&gt; Void' to expected argument type 'notify_handler_t!' (aka 'ImplicitlyUnwrappedOptional&lt;(Int32) -&gt; ()&gt;') 来了。
  • 现在Cannot convert value of type '(Int) -&gt; Void' to expected argument type 'notify_handler_t!' (aka 'ImplicitlyUnwrappedOptional&lt;(Int32) -&gt; ()&gt;') 来了。我将(_ token: Int) 更改为(_ token: Int32) 并且它可以正常工作,但是var state: UInt64 = UINT64_MAX notify_get_state(token, state) Cannot convert value of type 'UInt64' to expected argument type 'UnsafeMutablePointer&lt;UInt64&gt;!' 出现了。
  • 你必须提供地址,所以像这样:notify_get_state(token, &amp;state)
  • 好吧,我想通了,编辑您的答案并将var notify_token: Int32 放在函数声明之前。然后它将开始工作。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多