The problem happened when our app is written in Swift. In Swift, following function of SKYRecord.h will translate into non-null object (Any, rather than Any?):
Thus, the following will throw Expression implicitly coerced from 'String?' to 'Any'
let name: String? = nil
record.setObject(name, forKey: "name" as NSCopying)
Although inside SKYRecord.m it does handle the situation when object is nil (it create an NSNull object on fly). Since Swift doesn't want us to pass an optional, we are tamped to wrap it with an if-case:
if name != nil, !name.isEmpty {
record.setObject(name!, forKey: "name" as NSCopying)
} else {
record.setNilValueForKey("name")
}
Although we can provide an empty string like this: name ?? "", there are cases we cannot do so. For example, to unset an SKYReference, or unset an int field. However, this crash because SKYRecord.m does not provide implementation of setNilValueForKey:. It's better to provide the implementation and it would be as similar as calling
The problem happened when our app is written in Swift. In Swift, following function of
SKYRecord.h
will translate into non-null object (Any
, rather thanAny?
):Thus, the following will throw
Expression implicitly coerced from 'String?' to 'Any'
Although inside
SKYRecord.m
it does handle the situation whenobject
isnil
(it create anNSNull
object on fly). Since Swift doesn't want us to pass an optional, we are tamped to wrap it with an if-case:Although we can provide an empty string like this:
name ?? ""
, there are cases we cannot do so. For example, to unset an SKYReference, or unset an int field. However, this crash becauseSKYRecord.m
does not provide implementation ofsetNilValueForKey:
. It's better to provide the implementation and it would be as similar as callingMeanwhile, we can create an extension to overcome the problem:
Then we can also save the need of
as NSCopying
in Swift code: