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 DocumentType
s non-representable. To represent both optional and non-optional DocumentType
s, 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
Remove Element from Collection During Iteration with Foreach
What Are the Rules for Spaces in Swift
Whats the Swift Animate Withduration Syntax
Projecting the Arkit Face Tracking 3D Mesh to 2D Image Coordinates
How to Get User Home Directory Path (Users/"User Name") Without Knowing the Username in Swift3
Alamofire Asynchronous Completionhandler For Json Request
Retrieving Image from Firebase Storage Using Swift
Unowned Vs. Weak. Why We Should Prefer Unowned
How to Use Indices.Contains() in a Collection Extension in Swift 3
Swap Rootviewcontroller With Animation
Check If Key Exists in Dictionary of Type [Type:Type]
Troubles With Starting Value Using Uislider
Select() from Linq in Swift 3.0
Module Compiled With Swift 3.0 Cannot Be Imported in Swift 3.0.1
Reference to Property in Closure Requires Explicit 'Self.' to Make Capture Semantics Explicit