OGeek|极客世界-中国程序员成长平台

标题: ios - Objective-C中尖括号的含义? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:47
标题: ios - Objective-C中尖括号的含义?

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

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

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

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

我猜这部分是某种安全转换,以便返回的对象确认到练习日志。



Best Answer-推荐答案


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

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

它有一个通用参数。这就是

第二部分是对给定类型协议(protocol)的一致性。

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

引用文档:

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4