Custom class implementations set and get data based on array/dictionary literal syntax

Source: Internet
Author: User

. h file

#import <Foundation/Foundation.h> @interface testkvc:nsobject {nsmutabledictionary *mdictionary; 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

. m file

#import   "TestKVC.h" @implementation  TestKVC-  (ID) init {         self = [super init];    if  (self)  {         mArray = [NSMutableArray array];         mdictionary = [nsmutabledictionary dictionary];    }         return self;} -  (void) SetObject: (ID) Anobject atindexedsubscript: (Nsuinteger) index {    [ Marray insertobject:anobject atindex:index];} -  (ID) objectatindexedsubscript: (Nsuinteger) idx {    return [marray  OBJECTATINDEX:IDX];} -  (void) SetObject: (ID) Object forkeyedsubscript: (id < nscopying >) aKey {     [mdictionary setobject:object forkey:akey];} -  (ID) objectforkeyedsUbscript: (ID) key {    return [mdictionary objectforkey:key];} @end


Use

    TestKVC *test = [[TestKVC alloc] init];     test[@ "Key"] = @ "Hello";     id value = test[@ "key"];         [test setobject:@ "World"  forkeyedsubscript:@ "Key0"];     id value0 = [test objectforkeyedsubscript:@ "Key0"];         nslog (@ "%@ %@",  value, value0);  // hello  world            test[0] = @ "Hello";     test[1] = @ "World";        id  v0 = test[0];    id v1 = test[1];         nslog (@ "%@  %@",  v0, v1);          // hello woRld 


Custom class implementations set and get data based on array/dictionary literal syntax

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.