Objective-c How to implement a property access pattern based on array subscript

Source: Internet
Author: User

  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.