Hylke Bron
08/10/2022, 9:59 AMinterface KeyValueStorage {
@Throws(Throwable::class) fun get(key: String): String?
}
This generates the following in objective-c:
- (NSString * _Nullable)getKey:(NSString *)key error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("get(key:)")));
When you try to implement this in swift (as generated by xcode)
final class SampleStorage: KeyValueStorage {
func get(key: String) throws -> String? {
return ""
}
}
This function gives this compile error:
Throwing method cannot be an implementation of an @objc requirement because it returns a value of type 'String?'; return 'Void' or a type that bridges to an Objective-C class
Does anybody has any idea what the issue is, how to solve this, or have alternative suggestions?vanniktech
08/10/2022, 10:09 AMHylke Bron
08/10/2022, 11:07 AMRick Clephas
08/10/2022, 11:30 AMHylke Bron
08/10/2022, 11:49 AMRick Clephas
08/10/2022, 11:53 AMHylke Bron
08/10/2022, 11:59 AM@Throws(Throwable::class) contains(key: String): Boolean
conflicts with some protected keywords in objective-c (but we’re trying that now)Hylke Bron
08/10/2022, 12:00 PMThrowing method cannot be an implementation of an @objc requirement because it returns a value of type 'Bool'; return 'Void' or a type that bridges to an Objective-C class
Rick Clephas
08/10/2022, 12:00 PMswift_error
attribute in your ObjC, that could also explain something (if it really isn’t present).Hylke Bron
08/10/2022, 12:02 PMRick Clephas
08/10/2022, 12:06 PMUnit
returning function in Kotlin gives a BOOL
returning ObjC method?
The swift_error
attribute should be able to fix Swift from thinking a BOOL
return value is used for the error. But I am not sure if the Kotlin compiler actually adds it to the generated header.Rick Clephas
08/10/2022, 12:14 PMswift_error
is only added if the return type is nullable.Hylke Bron
08/10/2022, 12:28 PM@Throws(Throwable::class) fun delete(key: String)
objective c: - (*BOOL*)deleteKey:(NSString *)key error:(NSError * *_Nullable* * *_Nullable*)error *__attribute__*((swift_name("delete(key:)")));
swift: *func* delete(key: String) *throws* { }
object returning throwing function
kotlin: @Throws(Throwable::class) fun get(key: String): String
objective c: - (NSString * *_Nullable*)getKey:(NSString *)key error:(NSError * *_Nullable* * *_Nullable*)error *__attribute__*((swift_name("get(key:)")));
swift: *func* get(key: String) *throws* -> String { }
boolean returning throwing function
kotlin: @Throws(Throwable::class) fun contains(key: String): Boolean
objective-c: - (*BOOL*)contains:(NSString *)key error:(NSError * *_Nullable* * *_Nullable*)error *__attribute__*((swift_name("contains(key:)"))) *__attribute__*((swift_error(nonnull_error)));
swift: *func* contains(key: String) *throws* -> Bool { }
yields the error: Throwing method cannot be an implementation of an @objc requirement because it returns a value of type 'Bool'; return 'Void' or a type that bridges to an Objective-C class
Hylke Bron
08/10/2022, 12:31 PMRick Clephas
08/10/2022, 12:34 PMHylke Bron
08/10/2022, 2:58 PMRick Clephas
08/10/2022, 3:01 PMHylke Bron
08/10/2022, 3:08 PMcontains(key: String): Boolean
method so it no longer throws. So now it really only has 2 return options, either true or false (not true, false, exception anymore). So now the contains
method returns true if it is absolutely sure the value exists, and false if it is sure it does not exist, or some exception occurred (i.e. read write issues, storage being locked, or whatever propriatary checks or encryption the host app wishes to do).
And then I splitted the value getters in 2 methods:
• @Throws(Throwable::class) getOrThrow(key: String): String
which throws an exception if the value does not exist or if some issues with the underlying storage has occurred
• @Throws(Throwable::class) getOrDefault(key: String, valueIfAbsent: String): String
which still throws exceptions, but only if some issues with the underlying storage has occurred. If the value is just absent, it will return the default value
But as i said, this is a very domain specific solution, I think, every time you run into these limitations there are different considerations for the best solution.andylamax
08/10/2022, 3:15 PMiosMain
sourceSets
of the library???Hylke Bron
08/10/2022, 3:17 PMHylke Bron
08/10/2022, 3:18 PMHylke Bron
08/10/2022, 3:19 PMandylamax
08/10/2022, 3:19 PMiosMain
side of thingsandylamax
08/10/2022, 3:21 PMHylke Bron
08/10/2022, 3:22 PM