Home > Software engineering >  Swift 5 Method cannot be marked @objc because the type of the parameter 2 cannot be represented in O
Swift 5 Method cannot be marked @objc because the type of the parameter 2 cannot be represented in O

Time:03-18

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 static func makeShareLink( _ type: String, id: Int? = nil) -> String {
}

This class declaration is

@objc class Util: NSObject {
}

CodePudding user response:

You cannot represent an optional Int exposed to Objective C.

This will work:

@objc static func makeShareLink( _ type: String, id: Int) -> String {
    ...
}

If you want to use an optional number that can be exposed to Objective-C, this will work:

@objc static func makeShareLink( _ type: String, id: NSNumber? = nil) -> String {
    ...
}

The reason this works is because NSNumber is an object that can be nil, but an Int is not represented as an object in Objective C, but as a value.

  • Related