ObjectiveC常用容器示例代码

Environment: XCode 6.2

LastUpdate: kagula@2015-4-7


caller

void testContainers()
{
    //NSMutableArray equivalent vector/list in c++
    {
        MyNSItem *item1 = [[MyNSItem alloc] init];
        item1.myname = @"first";
        item1._value = 1;//access propery
        item1->nValue = 100;//access member variable
        //member varialble only can be used in the current inherit level object and interior.
        
        MyNSItem *item2 = [[MyNSItem alloc] init];
        item2.myname = @"second";
        item2._value = 2;
        item2->nValue = 200;
        
        MyNSItem *item3 = [[MyNSItem alloc] init];
        item3.myname = @"third";
        item3._value = 3;
        item3->nValue = 300;
        
        //print
        //if you do not want modify array, should be use NSArray instead.
        //NSMutableArray derived from NSArray interface.
        NSMutableArray* array = [NSMutableArray arrayWithObjects:item1,item2,item3, nil];
        NSEnumerator* enumerator = [array objectEnumerator];
        id anObject = [enumerator nextObject];
        while (anObject!=nil) {
            MyNSItem *item = (MyNSItem*)anObject;
            NSLog(@"%@ %d %d",item.myname,item._value,item->nValue);
            anObject = [enumerator nextObject];
        }

        //modify the specified index element.
        ((MyNSItem*)array[0])._value = 11;
        NSLog(@"After modify %d",((MyNSItem*)array[0])._value);
        
        //assign element
        array[0] = array[2];
        //array[0] = [[MyNSItem alloc]init];
        NSLog(@"After modify %d",((MyNSItem*)array[0])._value);
        
        //add element and output array size.
        [array addObject:[[MyNSItem alloc]init]];
        ((MyNSItem*)array[3])._value = 4;
        NSLog(@"After modify %d array.count=%ld",((MyNSItem*)array[3])._value, array.count);
        
        //remove element and output array size.
        [array removeObjectAtIndex:0];
        NSLog(@"After modify array.count=%ld",array.count);
    }
    
    
    //NSMutableDictionary equivalent std::map in c++ language
    {
        //you can use NSDictionary if you do not modify after create.
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MyFirstValue",@"FirstKey",@"MySecondValue",@"SecondKey", nil];
        
        NSLog(@"dict.count = %ld",dict.count);
        
        //add a element
        dict[@"MyNewKey"]=@"MyNewValue";
        NSLog(@"dict.count = %ld",dict.count);
        
        //iterate key
        NSEnumerator* enumeratorKey = [dict keyEnumerator];
        for (NSObject *obj in enumeratorKey) {
            NSLog(@"key = %@ %@",obj,dict[obj]);
        }
        
        //iterate name
        NSEnumerator* enumeratorV = [dict objectEnumerator];
        for (NSObject *obj in enumeratorV) {
            NSLog(@"value = %@",obj);
        }
        
        //find key if exist
        NSObject* object = [dict objectForKey:@"FirstKey"];
        if (object!=nil) {
            //get value by key
            NSLog(@"Get Value by key %@ %@",dict[@"FirstKey"],object);
        }
        
        //remove a element
        [dict removeObjectForKey:@"FirstKey"];
        NSLog(@"dict.count = %ld",dict.count);
    }
    
    //NSMutableSet equivalent std::set in C++.
    {
        NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"one",@"two",@"three",nil];
        NSLog(@"set size = %ld",set.count);
        
        //if the element exist?
        BOOL bExist = [set containsObject:@"one"];
        NSLog(@"bExist=%@",bExist?@"true":@"false");
        
        //remove the element.
        [set removeObject:@"one"];
        bExist = [set containsObject:@"one"];
        NSLog(@"bExist=%@",bExist?@"true":@"false");
        
        //add the element
        [set addObject:@"one"];
        bExist = [set containsObject:@"one"];
        NSLog(@"bExist=%@",bExist?@"true":@"false");
    }
}


Ancestor.h

//
//  Ancestor.h
//  example
//
//  Created by junli on 15/4/7.
//  Copyright (c) 2015年 junli. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Ancestor : NSObject
{
@public
//    int _value;
}
@property NSString *myname;
@property int _value;
-(void)live;
@end


Ancestor.m

#import "Ancestor.h"

@implementation Ancestor
-(void)live
{
    NSLog(@"Ancestor is living inside!");
}
@end


MyNSItem.h

//
//  MyNSItem.h
//  example
//
//  Created by junli on 15/4/7.
//  Copyright (c) 2015年 junli. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Ancestor.h"

@interface MyNSItem : Ancestor
{
@public
    int nValue;
}
@end


MyNSItem.m

#import "MyNSItem.h"

@implementation MyNSItem
-(id)init
{
    if (self=[super init]) {
        super.myname = @"";//only property can be access in the derived interface.
        super._value = 0;//for can not directly access parent's member variable.
        nValue = 0;
    }
    return self;//self like this notation in c++.
}
@end


你可能感兴趣的:(ObjectiveC常用容器示例代码)