after iOS6.0 and OS X10.8, Apple introduced a set of informal protocols (informal protocol) that are directly bound to the OBJECTIVE-C syntax. Once you have implemented this method, you can use the array subscript to access the attribute elements        in the Foundation library, the Nsarray class implements the-(ID) Objectatindexedsubscript: (Nsuinteger) Idx method. Therefore, we can access the array elements as follows: Nsarray *arr = @[@100, @200, @300]; NSNumber *num = arr[0];  above arr[0] is equivalent to [arr objectatindex:0].    Nsmutablearray on the basis of Nsarray-(void) SetObject: (ID) anobject atindexedsubscript: (Nsuinteger) The index method. This allows us to read and write the corresponding elements through array subscripts, such as:  nsmutablearray *arr = [Nsmutablearray arraywitharray:@[@100, @200, @300]];arr[2] = arr[0] ;  and the Nsdictionary class implements the-(ID) Objectforkeyedsubscript: (ID) key method. This allows us to access the value of the corresponding key in the form of an array subscript. For example:  nsdictionary *dict = @{@ "key": @ "value"}; NSString *value = dict[@ "Key"], and Nsmutabledictionary is implemented on the basis of Nsdictionary class-(void) SetObject: (ID) object Forkeyedsubscript: (ID < nscopying >) Akey method. In this way, we can read and write the values of the corresponding keys in an array subscript. For example:  nsmutabledictionary *dict = [nsmutabledictionary dictionarywithdictionary:@{@ "key": "@Hello"}];d ict[dict[@ "Key"]] = @ "World";    below we implement these four methods to implement a class that can use these four subscript modes to access the schema at the same time.    
 
  
   
   |   main.m//  objctest////  Created by Zenny Chen on 12-2-7.//  Copyright (c) 2014 Neon Media Studi O. All rights reserved.//  #import <Foundation/Foundation.h>   @interface mycontainer:nsobject{@ private       nsmutabledictionary *mdict;    NSMutableArray *mArray;}  -(void) SetObject: (ID) object forkeyedsubscript: (ID < nscopying >) akey;-(ID) Objectforkeyedsubscript: (ID) key;-(void) SetObject: (ID) anobject atindexedsubscript: (Nsuinteger) index;-(ID) Objectatindexedsubscript: ( Nsuinteger) idx;  @end   @implementation mycontainer -(instancetype) init{    self = [ Super init];       mdict = [[Nsmutabledictionary alloc] initwithdictionary:@{@ "Key1": @ "Value1" @ "Key2": @ "value2"}];       marray = [[Nsmutablearray alloc] initwitharray:@[ @100, @200, @300, @400]];       return self;}  -(void) dealloc{    if (mdict! = nil)     {        [ Mdict removeallobjects];        [Mdict release];         mdict = nil;   }       if (marray! = nil)     {        [Marray removeallobjects];        [Marray release];        marray = nil;   }        [Super Dealloc];}  -(void) SetObject: (ID) object forkeyedsubscript: (ID < nscopying >) akey{    [mdict setobject : Object Forkey:akey];}  -(ID) Objectforkeyedsubscript: (ID) key{    return [mdict Objectforkey:key];}  -(void) SetObject: (ID) anobject atindexedsubscript: (nsuinteger) index{    const NSUInteger length = [Marray count];    if (index > Length)         return;       if (index = = Length)         [Marray addobject:anobject];    else         [Marray replaceobjectatindex:index withobject:anobject];}  -(ID) objectatindexedsubscript: (Nsuinteger) idx{    if (idx >= [Marray count])          return nil;       return [Marray objectatindex:idx];}   @end   int Main (int argc, const char * argv[]) {    @autoreleasepool     {& nbsp;      //Insert Code here...                MyContainer *cont = [[MyContainer alloc] init];                cont[@ "MyKey"] = @ "Myvalye";                 NSLog (@ "Key1 is:%@", cont[@ "Key1"]);         NSLog (@ "Key2 is:%@", cont[@ "Key2"]);        NSLog (@ "MyKey are: %@ ", cont[@" MyKey "]);               cont[4 ] = @500;        cont[2] = @-300;                NSLog (@ "the value[4] =%@", cont[4]);         NSLog (@ "the value[3] =%@", cont[3]);        NSLog (@ "the value[2] =%@" , cont[2]);   }       return 0;} |  
  
 
  
 
Objective-c How to implement a property access pattern based on array subscript