Reputation: 9120
I'm working in swift framework but I need to support ObjC. But I have a function like this:
@objc public func doingSomething(thing:String?,time:Int?)->String{
return result
}
But I'm getting the following error:
Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C
Any of you knows a work around this error?, or how can I fix this error?
I'll really appreciate your help.
Upvotes: 1
Views: 692
Reputation: 17040
The problem is that integers are a primitive type in Objective-C, so the language is not capable of assigning nil
to an integer-typed variable. Therefore, you cannot have Int?
in the signature of a method that is exposed to Objective-C.
As a workaround, you can either make the parameter an Int
and define a sentinel value to represent a nil value (NSNotFound
is an example found in the various "indexOf" methods in the frameworks; 0
may work in some cases), or else you can type the parameter as an NSNumber?
since NSNumber
is an object type and thus can be nil
.
Upvotes: 5