Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

Tags:

swift

After I have updated Swift 1 to Swift 2.0 I have an issue.

I am getting the following error on the first line of this code:

Method cannot be marked @objc because the type of the parameter cannot be represented in 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 }  

This this Person class:

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 } 
like image 555
J A S K I E R Avatar asked May 16 '16 14:05

J A S K I E R


2 Answers

You have very nicely explained the problem yourself:

class Person: Hashable { 

Person is not an NSObject. But only an NSObject-derived class type can be seen by Objective-C. Therefore your Person type is invisible to Objective-C. But your @objc func declaration is for a function that takes an array of Person — and we have just said that Person is invisible to Objective-C. So your @objc func declaration is illegal. Objective-C cannot be shown this function, because it cannot be shown its parameter.

You would need to change your class declaration to start like this:

class Person: NSObject { 

...and then you might of course have to make any necessary further adjustments in the class's implementation. But that change would make your @objc func declaration legal. (NSObject is Hashable, so the amount of work needed to make this adaptation might not be very great.)

like image 127
matt Avatar answered Sep 20 '22 12:09

matt


I was getting this because I declared a class Notification of my own and it was messing with Foundation's Notification class.

@objc func playerItemDidReachEnd(notification: Notification) {...} 

So I changed it to Foundation.Notification

@objc func playerItemDidReachEnd(notification: Foundation.Notification) {...} 
like image 44
iOS Developer Avatar answered Sep 20 '22 12:09

iOS Developer