Swift 2.0方法无法标记为@objc,因为参数的类型无法在Objective-C中表示

Ole*_*ndr 24 swift

在我将Swift 1更新为Swift 2.0后,我遇到了一个问题.

我在这段代码的第一行收到以下错误:

方法不能标记为@objc,因为参数的类型不能在Objective-C中表示

@objc func personsToFirstStep(persons: [Person]) {
    for person in persons {
        if !self.persons.contains(person) && person.id != userID {
            self.persons.append(person)
        }
    }

    collectionView.reloadData()
    collectionViewPlaceholder.hidden = true
    collectionView.hidden = false
    collectionGradientView.hidden = false
} 
Run Code Online (Sandbox Code Playgroud)

这个Person类:

class Person: Hashable {

    var intID: Int = 0
    var id: String = ""
    var name: String = ""
    var type: String = ""

    var hashValue: Int {
        return self.intID
    }

    init(id: String, name: String, type: String) {
        self.id = id
        self.intID = Int(id)!
        self.name = name
        self.type = type
    }

}

func ==(lhs: Person, rhs: Person) -> Bool {
    return lhs.intID == rhs.intID
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 35

你自己很好地解释了这个问题:

class Person: Hashable {
Run Code Online (Sandbox Code Playgroud)

人不是NSObject.但Objective-C只能看到NSObject派生的类类型.因此,您的Person类型对Objective-C是不可见的.但是你的@objc func声明是针对一个带有Person数组的函数 - 我们刚才说Person对Objective-C是不可见的.所以你的@objc func声明是非法的.Objective-C无法显示此功能,因为它无法显示其参数.

您需要将类声明更改为以下内容:

class Person: NSObject {
Run Code Online (Sandbox Code Playgroud)

......然后你当然可以在班级的实施中做出任何必要的进一步调整.但这一改变将使您的@objc func声明合法化.(NSObject Hashable,所以进行这种调整所需的工作量可能不是很大.)


iOS*_*per 6

我得到这个是因为我声明了一个Notification我自己的类,它与 Foundation 的 Notification 类混淆。

@objc func playerItemDidReachEnd(notification: Notification) {...}
Run Code Online (Sandbox Code Playgroud)

所以我把它改成了 Foundation.Notification

@objc func playerItemDidReachEnd(notification: Foundation.Notification) {...}
Run Code Online (Sandbox Code Playgroud)