gpt4 book ai didi

ios - swift 类别 : "Cannot convert value of type to expected argument type ' AnyObject! '

转载 作者:行者123 更新时间:2023-11-30 13:55:22 26 4
gpt4 key购买 nike

我正在尝试将 Objective C 中的类别转换为 swift。我什至对 Objective C 类别都不太熟悉,现在我收到错误:“无法转换‘SelectionBlock’类型的值!”到预期的参数类型“AnyObject!””,我不知道从这里去哪里。

import Foundation
import MapKit
import ObjectiveC

var AssociatedObjectHandle: UInt8 = 0

extension MKPointAnnotation {

typealias SelectionBlock = () -> Void

var selectionBlock: SelectionBlock! {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! SelectionBlock
}
set(selectionBlock) {
objc_setAssociatedObject(self, &AssociatedObjectHandle, selectionBlock, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
}
}

}

最佳答案

Caroline,尝试强制将选择 block 放入 AnyObject

extension MKPointAnnotation {

typealias SelectionBlock = () -> Void

var selectionBlock: SelectionBlock! {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! SelectionBlock
}
set(selectionBlock) {
objc_setAssociatedObject(self, &AssociatedObjectHandle, selectionBlock as! AnyObject, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
}
}

}

关于ios - swift 类别 : "Cannot convert value of type to expected argument type ' AnyObject! ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com