Object subscripting in objc

Object scripting what use something other than an integer inside []. For example, dictionary[@”key”] = @”value”

And in generated machine code, it is a call to selector

-(id) objectForKeyedSubscripting:(id)key

An informal protocol named __ARCLiteKeyedSubscripting__ was defined

@protocol __ARCLiteKeyedSubscripting__
– (void)setObject:(id)arg1 forKeyedSubscript:(id)arg2;
– (id)objectForKeyedSubscript:(id)arg1;
@end

Built-in types like NSDictionary already implemented this protocol, which is indeed generated by compiler. For custom types, just implemented those 2 methods and everything will work.

Leave a Reply

Your email address will not be published. Required fields are marked *