gpt4 book ai didi

objective-c - RLMArray 属性声明语法

转载 作者:行者123 更新时间:2023-12-02 08:20:08 25 4
gpt4 key购买 nike

当声明一个 RLMArray 时,第二组括号的意义是什么? Realm 是我见过的唯一用过的地方。

@property NSArray<NSDictionary*> *dictionaries;  // I understand this (and it's wonderful!)
@property NSDictionary<NSString*, NSArray<NSString*>*> *dictionaryOfArraysOfStrings; // No problem with this either
@property RLMArray<Object*><Object> *objects; // What is <Object> for?

最佳答案

两组尖括号分别用于 Objective-C 泛型和协议(protocol)。 Objective-C 泛型让编译器知道像 -[RLMArray firstObject] 这样的方法返回数组包含的特定类型的对象,而不是任何可能的 RLMObject 子类。遗憾的是,这些额外的类型信息在运行时会被删除,因此 Realm 无法通过在属性声明中单独使用 Objective-C 泛型来判断数组包含什么类型的对象。这就是协议(protocol)一致性的用武之地。属性符合的协议(protocol) 在运行时可用于 Realm,因此用于通知 Realm 您的 RLMArray 属性将包含。 Realm provides the RLM_ARRAY_TYPE macro声明一个与模型类同名的协议(protocol),所以很容易错过涉及的协议(protocol)。

关于objective-c - RLMArray 属性声明语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37984784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com