首页 > 代码库 > 字典,集合练习

字典,集合练习

#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 =http://www.mamicode.com/[dicl allValues];"%@",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

 

字典,集合练习