小编byo*_*hay的帖子

检查ObjC属性在运行时是否可为空

我试图在运行时弄清楚类的属性是否可以为空.例如:

@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为我需要知道的可空属性的每个类实现.

nullable introspection objective-c declared-property

2
推荐指数
1
解决办法
286
查看次数