Swift 2.0 Method Cannot Be Marked @Objc Because the Type of the Parameter Cannot Be Represented in Objective-C

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

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

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

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.

Method cannot be a member of an @objc protocol because its result type cannot be represented in Objective-C

Tuples cannot be represented in Obj-C, so you cannot make your function return a tuple. Create an Obj-C type that holds both fields you want to return and return that type instead of a tuple.

@objc public class ShareSecurityQuestionResult: NSObject {
let success: Bool
let error: String

init(success: Bool, error: String) {
self.success = success
self.error = error
}
}

@objc public protocol ShareSecurityQuestionProtocol {
func setResultofSecurityQuestion(valueSent: [NSMutableDictionary]) -> ShareSecurityQuestionResult
}

Passing optional @objc enum type into @objc protocol

Objective-C doesn't have optional enums. Enums must be non-optional. Classes can be optional, not enums :(

One workaround is to add a case:

@objc enum DocumentType: Int {
case pdf
case png
case none
}

And use the non-optional type DocumentType instead.

Of course, this makes non-optional DocumentTypes non-representable. To represent both optional and non-optional DocumentTypes, you would need two types:

@objc enum DocumentType: Int {
case pdf
case png

func asOptionalDocumentType() -> OptionalDocumentType {
switch self {
case .pdf: return .pdf
case .png: return .png
}
}
}

extension Optional where Wrapped == DocumentType {
func asOptionalDocumentType() -> OptionalDocumentType {
self?.asOptionalDocumentType() ?? .none
}
}

@objc enum OptionalDocumentType: Int, ExpressibleByNilLiteral {

case pdf
case png
case none

func asDocumentType() -> DocumentType? {
switch self {
case .pdf: return .pdf
case .png: return .png
case .none: return nil
}
}

init(nilLiteral: ()) {
self = .none
}
}

I have added conversion methods to make it easy to convert between them, but they are not technically needed.

Unable to use custom class in a protocol with @objc attribute?

What the 3 methods have in common is the JSONLoader parameter, and that's what I think prevents you from bridging the protocol. In order to solve the problem you have to make it objc compatible.



Related Topics



Leave a reply



Submit