iOS 如何在自定义类中支持 "[]" 运算符

在相应类中实现如下协议即可。

1、字典类

- (id)objectForKeyedSubscript:(NSObject <NSCopying> *)key;

- (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key;

someObject[@"xxx"] = someValue

2、数组类

- (id)objectAtIndexedSubscript:(NSUInteger)index;

- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;

someObject[0] = someValue

 

posted @ 2018-04-02 15:11  茄菲兔  阅读(128)  评论(0编辑  收藏  举报