最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中dispatch_once出错:& used with non-inout argument of type UnsafeMutablePointer (aka UnsafeMutablePointer)

Swift crifan 2815浏览 0评论

代码:

class ImageBubbleView: UIView {
 
    static var refreshImgToken:dispatch_once_t = 0
    func doItOnce() {
        dispatch_once(&self.refreshImgToken) {
            print("Do it once")
        }
    }
}

出错:

‘&’ used with non-inout argument of type ‘UnsafeMutablePointer<dispatch_once_t>’ (aka ‘UnsafeMutablePointer<Int>’)

如图:

搜:

& used with non-inout argument of type UnsafeMutablePointer<dispatch_once_t> (aka UnsafeMutablePointer<Int>)

ios – Swift error: ‘&’ used with non-inout argument of type ‘UnsafeMutablePointer’ – Stack Overflow

swift – How to create a UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<Int8>>> – Stack Overflow

dispatch_once & used with non-inout argument

ios – Cannot invoke ‘dispatch_once’ with an argument list of type – Stack Overflow

也去看了源码:

/*!
 * @function dispatch_once
 *
 * @abstract
 * Execute a block once and only once.
 *
 * @param predicate
 * A pointer to a dispatch_once_t that is used to test whether the block has
 * completed or not.
 *
 * @param block
 * The block to execute once.
 *
 * @discussion
 * Always call dispatch_once() before using or testing any variables that are
 * initialized by the block.
 */

@available(iOS 4.0, *)
public func dispatch_once(predicate: UnsafeMutablePointer<dispatch_once_t>, _ block: dispatch_block_t)

public func _dispatch_once(predicate: UnsafeMutablePointer<dispatch_once_t>, _ block: dispatch_block_t)
public func __builtin_expect(_: Int, _: Int) -> Int

@available(iOS 4.0, *)
public func dispatch_once_f(predicate: UnsafeMutablePointer<dispatch_once_t>, _ context: UnsafeMutablePointer<Void>, _ function: dispatch_function_t)

public func _dispatch_once_f(predicate: UnsafeMutablePointer<dispatch_once_t>, _ context: UnsafeMutablePointer<Void>, _ function: dispatch_function_t)

是这么使用的啊。。。

但是为何还是出错?

很诡异

最后改为:

class ImageBubbleView: UIView {
    static var refreshImgToken:dispatch_once_t = 0
    init(frame:CGRect, resourceMessage:ResourceMessage){
        ImageBubbleView.doOnceUpdateMessageCell(self.resMsg)
        }
    class func doOnceUpdateMessageCell(resMsg:ResourceMessage) {
        dispatch_once(&self.refreshImgToken) {
            delayDispatchInBackgroundThread(0.2, thingsTodo: {
                if let relatedContactId = getRelatedContactId(resMsg) {
                    if let conversationItem = SingletonConversationTVC().getConversationItemFromId(relatedContactId) {
                        let msgTVC = getMsgTVCFromItem(conversationItem.contactItem)
                        msgTVC.updateMessage(resMsg)
                    }
                }
            })
        }
    }

然后就可以了。。。

彻底无语了。

需要搞个Class func 才能调用那个token。。。

转载请注明:在路上 » [已解决]swift中dispatch_once出错:& used with non-inout argument of type UnsafeMutablePointer (aka UnsafeMutablePointer)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.179 seconds, using 22.09MB memory