Objective-C 字典

#import <Foundation/Foundation.h>


int main(int argc,const char * argv[])

{


    @autoreleasepool {

        

        

       //是一系列  “——的集合

       //创建字典:dictionaryWithObjectsAndkeys:

        NSDictionary *glossary = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInteger:97],@"nicky",

            [NSNumbernumberWithInteger:95],@"johnson",

            [NSNumbernumberWithInteger:90],@"black",

            [NSNumbernumberWithInteger:100],@"lily",nil];//常量字典不可变

        

           NSLog(@"%@",glossary);

        

        //查找到对象:objectForKey:参数s关键字,返回的是值。

       NSLog(@"%@",[glossaryobjectForKey:@"johnson"]);//键查值

        

        ///可变字典NSMutableDictionary///

        //创建新的可变字典:dictionaryWithCapacity:

        //给字典添加元素:setObject:forKey:

        //如关键字已存在则替换原有的值

        //删除指定d元素:removeObjectForKey;

        

        NSMutableDictionary *mGlossary = [NSMutableDictionarydictionary];

        

        [mGlossary setObject:[NSNumbernumberWithInteger:97]forKey:@"nicky"];

        

        [mGlossary setObject:[NSNumbernumberWithInteger:95]forKey:@"johnson"];

        

        [mGlossary setObject:[NSNumbernumberWithInteger:90]forKey:@"black"];

        [mGlossary setObject:[NSNumbernumberWithInteger:100]forKey:@"lily"];

        

        [mGlossaryremoveObjectForKey:@"johnson"];

        

        NSMutableDictionary *my=[NSMutableDictionarydictionaryWithCapacity:20];//预估字典个数

        

        

        NSLog(@"----------------------");

        //对字典请求枚举器///

        //遍历字典中的所有对象,取的是值

        

       NSEnumerator *enumerator;

        enumerator = [glossaryobjectEnumerator];

       id obj;

       while (obj = [enumerator nextObject]) {

           NSLog(@"I found %@",obj);

        }

        

        //遍历字典中的所有关键字

       NSEnumerator *enumerator1;

        enumerator1 = [glossarykeyEnumerator];

       id obj1;

       while (obj1 = [enumerator1 nextObject]) {

           NSLog(@"I found %@",obj1);

        }

        

       //快速枚举

        //对字典中进行快速枚举会遍历数组中的每个关键字

       for(id key in glossary)

        {

           NSLog(@"I found %@",key);

        }

        


    

        

        

        

        

        

    }

   return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值