I'm dealing with an issue where Obj-C code can pass nil to Swift methods that do not take Optional parameters.
I'm looking for a way for Xcode compiler to prevent this. It seems that all nullability annotations are for Swift to call Objc, not vice-versa.
Some example code:
// file1.swift
@objc
class Foo: NSObject {
@objc let date: Date? // default to nil
...
}
// file2.swift
extension Date {
@objc(doSomethingWithOtherDate:)
func doSomething(with otherDate: Date) {
print(date)
}
}
Then in Obj-c, we call the Swift method passing in an optional value, with nil:
// file3.m
[Date.new doSomethingWithOtherDate:[Foo.new date]];
Is there a way to prevent this? How can we tell the compiler doSomethingWithOtherDate does not take nil.
Follow up
Following up on this issue, passing a literal nil on the call triggers a warning, which is very helpful. However, passing a nil value does not trigger anything. The static analyzer does however find the issue if the nil reference is local.
However, if a nil reference is passed via another object, the static analyzer fails to identify this issue.
I've made this repo to showcase this issue: https://github.com/eneko/ObjcCrashDemo
