代码:
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
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) @available(iOS 4.0, *) 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)