当我试图覆盖一个getter时,这个语法是什么? [英] What is this syntax when I was attempting to override a getter?

查看:252
本文介绍了当我试图覆盖一个getter时,这个语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图覆盖一个getter时,这个语法是什么?

What is this syntax when I was attempting to override a getter??

我只是想弄清楚如何在Objective-C中了解属性的工作原理。这是我的财产:

I'm just messing around trying to learn more about how properties work in Objective-C. Here is my property:

@property (nonatomic, strong) UIView *myView;

当我尝试覆盖getter时,我得到了这个帮助:

When I try to override the getter I get this help:

-(void)getMyView:(<object-type> **)buffer range:(NSRange)inRange
{

}

我知道我可以使用这个:

I know I can use this:

-(UIView *)myView
{

}

但我很好奇以前的方法做了什么,为什么它在那里,等等。感谢您的帮助!

But I am just curious as to what the previous method does, why it's there, etc. Thanks for any help!

推荐答案

它被称为Getter索引访问器,如键值编码编程指南

It's called "Getter Indexed Accessors" as explained in the Key-Value Coding Programming Guide

来自文档:


为了支持对有序多对多关系的只读访问,请实现以下方法:

In order to support read-only access to an ordered to-many relationship, implement the following methods:

-countOf<钥匙及GT; 必需。这类似于NSArray原始方法计数。

-countOf<Key> Required. This is the analogous to the NSArray primitive method count.

-objectIn< Key> AtIndex: - < key> AtIndexes:必须实现其中一种方法。它们对应于NSArray方法 objectAtIndex: objectsAtIndexes:

-objectIn<Key>AtIndex: or -<key>AtIndexes: One of these methods must be implemented. They correspond to the NSArray methods objectAtIndex: and objectsAtIndexes:

-get< Key>:range:实现此方法是可选的,但可以提供额外的性能提升。此方法对应于 NSArray 方法 getObjects:range:

-get<Key>:range: Implementing this method is optional, but offers additional performance gains. This method corresponds to the NSArray method getObjects:range:.

您可以出于性能原因实施此类方法,如指南中所述

You can implement such methods for performance reasons, as explained in the guide


如果基准测试表明要求性能改进,您还可以实现 -get< Key>:range:。这个访问器的实现应该在缓冲区中返回,作为第一个参数给出的对象落在第二个参数指定的范围内。

If benchmarking indicates that performance improvements are required, you can also implement -get<Key>:range:. Your implementation of this accessor should return in the buffer given as the first parameter the objects that fall within the range specified by the second parameter.

例如

- (void)getEmployees:(Employee * __unsafe_unretained *)buffer range:(NSRange)inRange {
    // Return the objects in the specified range in the provided buffer.
    // For example, if the employees were stored in an underlying NSArray
    [self.employees getObjects:buffer range:inRange];
}

这篇关于当我试图覆盖一个getter时,这个语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆