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

标签 ios objective-c

尽管问题很广泛,但我实际上对我最近在使用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/

相关文章:

ios - 为纵向和横向模式获得相同的屏幕宽度和高度

ios - UIAutomation 不适用于 IPA 的分发类型

ios - UIPageViewController 以编程方式允许调用数据源

ios - 无法将 RGB 转换为 UIColor

ios - 将 GMSVisibleRegion 转换为 CLRegion 或 MKCoordinateRegion

ios - 如何从 KISSXML 中删除 xml 声明?

ios - 每个点都没有调用 ScrollviewDidScroll

php - 纬度和经度值保存为字符串还是数字?

ios - 从 JSQMessagesViewController 打开图像

ios - 委托(delegate)模式,OOP 中界面设计的奇特名称?