我试图在运行时弄清楚类的属性是否可以为空.例如:
@interface A : NSObject
+ (NSSet<NSString *> *)nullableProperties;
@property (readonly, nonatomic) NSString *description;
@property (readonly, nonatomic, nullable) NSError *error;
@end
@implementation A
+ (NSSet<NSString *> *)nullableProperties {
// Code that identifies nullable properties.
}
@end
Run Code Online (Sandbox Code Playgroud)
nullableProperties应该在这种情况下,返回一个NSSet有@"error".
property_getAttributesfunction可以提供一些属性的信息(这里有更多信息).不幸的是,它没有提供有关该属性是否被声明为可空的信息.
我想避免nullableProperties为我需要知道的可空属性的每个类实现.