我正在尝试将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)
        }
    }

}

最佳答案

卡罗琳,尝试强迫进行选择

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)
        }
    }

}

08-06 13:35