gpt4 book ai didi

ios - Objective-C 中尖括号的含义?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:57:41 26 4
gpt4 key购买 nike

尽管问题很广泛,但我实际上对我最近在使用 Realm library 播种的一个案例感到好奇.因为我以前在很多场合使用过协议(protocol)(委托(delegate))并且还使用 <> 导入了类。现在这是我不完全理解或根本不理解的代码行:

@property (nonatomic, strong) RLMArray <ExerciseLog *><ExerciseLog> * exerciseLogs;

我假设 <ExerciseLog> * exerciseLogs 行的第二部分用于确保exerciseLogs可以是任何符合ExerciseLog协议(protocol)的ExerciseLog的实例,我的假设是否正确?

或者简单地说,如果用户发送的对象与预期对象不同,应用程序不会崩溃,并且会分配一个默认值。

我猜这部分是某种安全转换,以便返回的对象与 ExerciseLog 确认。

最佳答案

Obj-C 协议(protocol)一致性和泛型的结合。 RLMArray声明为

@interface RLMArray < RLMObjectType : RLMObject * >  : NSObject<RLMCollection,NSFastEnumeration> 

它有一个通用参数。那是 <ExerciseLog *> .

第二部分<ExerciseLog>符合给定类型的协议(protocol)。

顺便说一句,该协议(protocol)是使用 RLM_ARRAY_TYPE 声明的宏。代码似乎有点复杂,但它可能是强制执行数组元素类型的旧方法( RLMArray<protocolX> 不可分配给 RLMArray<protocolY> )。

引用文档:

Unlike an NSArray, RLMArrays hold a single type, specified by the objectClassName property. This is referred to in these docs as the “type” of the array.

When declaring an RLMArray property, the type must be marked as conforming to a protocol by the same name as the objects it should contain (see the RLM_ARRAY_TYPE macro). RLMArray properties can also use Objective-C generics if available. For example:

关于ios - Objective-C 中尖括号的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851086/

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