Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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;

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!

like image 558
SirRupertIII Avatar asked Oct 30 '13 02:10

SirRupertIII


1 Answers

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

From the documentation:

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

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

-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: 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

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.

As an example

- (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];
}
like image 132
Gabriele Petronella Avatar answered Nov 08 '22 08:11

Gabriele Petronella