-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Description
I use @available(...) for computed vars that are only available on some OS versions. This works until I add @objc to access the property from objc. Once I add @objc the property produces a warning in the generated header file like "MyClass" is only available on iOS 13.0 or newer
.
This appears to happen based on the name used for the type annotated with @available and the name of the class containing the accessor. If the unavailable type us alphabetically before the type containing the property it will be ordered before it in the generated header file, and the compiler will see it instead of the forward declaration, leading to the error.
Reproduction
This code causes the unexpected warning:
@objc public class TestClass2: NSObject {
@available(iOS 13.0, *)
@objc public static var testClass: TestClass1 {
TestClass1()
}
}
@available(iOS 13.0, *)
@objc public class TestClass1: NSObject {}
This code does not:
@objc public class TestClass0: NSObject {
@available(iOS 13.0, *)
@objc public static var testClass: TestClass1 {
TestClass1()
}
}
@available(iOS 13.0, *)
@objc public class TestClass1: NSObject {}
The only difference here is "TestClass0" is alphabetically before "TestClass1" so now the generated header uses a forward declared @class TestClass1
and does not know it is unavailable by the time it reads the accessor. The generated code that causes the warning looks like this:
SWIFT_CLASS("_TtC11TestingFmwk10TestClass2")
@interface TestClass2 : NSObject
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) TestClass1 * _Nonnull testClass SWIFT_AVAILABILITY(ios,introduced=13.0);)
+ (TestClass1 * _Nonnull)testClass SWIFT_WARN_UNUSED_RESULT;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
Expected behavior
No warning should be produced in the objc code, especially not a warning that only depends on the alphabetical ordering of these classes. The header file should probably have SWIFT_AVAILABILITY(ios,introduced=13.0)
on the generated function's line in addition to the SWIFT_CLASS_PROPERTY
line.
Environment
I'm using Xcode 16.4
Additional information
This bug relates to objc interop so I wasn't sure if it belongs here or bugreport.apple.com, I can create a report there if that's a better home for it.