码迷,mamicode.com
首页 > 其他好文 > 详细

字典,集合练习

时间:2014-10-16 22:35:33      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   div   on   cti   

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array1 =[NSArray arrayWithObjects:@"wangwei",@"wangwangwei",nil];
    NSArray *array2 =[NSArray arrayWithObjects:@"zhangsan",@"zhangzhang",nil];
    NSDictionary *dicl =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"wang",array2,@"zhang" ,nil];
    //获取字典中元素的个数
    NSUInteger count =dicl.count;
    NSLog(@"count:%ld",count);
    NSDictionary *dicl1 = [NSDictionary dictionaryWithObject:array1 forKey:@"wang"]; //创建字典初始化一个元素
    //获取字典中的所有key
    NSArray *allkeys =[dicl allKeys];
    NSLog(@"%@",allkeys);
    //获取所有的value
    NSArray *allvalue =[dicl allValues];
    NSLog(@"%@",allvalue);
    //通过key取value
      NSArray *array3=[dicl objectForKey:@"wang"];
    NSLog(@"%@",array3);
    NSArray *array4 =[dicl objectForKey:@"zhang"];
    NSLog(@"------------%@---------------",array4);
    
    NSDictionary *dicl2 = @{@"wang":array1 ,@"zhang":array2};  //优化后的创建法
    NSLog(@"XXXXXXXXXXXXXX%@XXXXXXXXXXXX",dicl2);
    
    
    NSArray *array5 =dicl2 [@"zhang"];     //通过key取value
    NSLog(@"array5= %@",dicl2);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 集合练习

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional set  NSSet up after loading the view, typically from a nib.
    NSString *s1 =@"zhangsan";
    NSString *s2 =@"wangwei";
    //创建NSSet对象
    NSSet *set1 =[[NSSet alloc]initWithObjects:s1,s2, nil];
    NSSet *set2 =[NSSet setWithObjects:s1,s2,nil];
    //把数组中的元素全部取出来,存到set 对象中
   // NSSet *set3 =[NSSet setWithArray:<#(NSArray *)#>];
    //NSSet转成数组
    NSArray *array1 =[set1 allObjects];
    //返回元素的个数
    NSUInteger count =[set1 count];//set1.count;
    //从容器中随机获取某个元素
    NSString *s3 =[set1 anyObject];
    //NSSet中不能存重复的对象
    NSString *str =@"jack";
    NSArray *array2 =[NSArray arrayWithObjects:str ,str,nil];
    NSLog(@"%@",array2);
    NSSet *set4=[NSSet setWithObjects:str, str,nil];
    NSLog(@"%@",set4);
    
    return ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

字典,集合练习

标签:blog   io   os   ar   for   sp   div   on   cti   

原文地址:http://www.cnblogs.com/wangweixl/p/4029721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!