foundation-NSDictonary&NSMutableDictionary

本文探讨了Objective-C与Swift两种编程语言在iOS开发领域的应用与区别,介绍了它们各自的优势与特点,以及如何在实际项目中选择合适的语言进行开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  main.m
//  Foundation-NSDectionary
//
//  Created by apple on 15/6/30.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h";

#pragma mark 字典的创建
void dictCreate(){
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
    //最常用的初始化方式
    dic = [NSDictionary dictionaryWithObjectsAndKeys:
           @"v1",@"k1",
           @"v2",@"k2",
           @"v3",@"k3",
           @"v4",@"k4",nil];
    NSLog(@"%@",dic);
    
    NSArray *objects = [NSArray arrayWithObjects:@"v7",@"v8",@"v9", nil];
    NSArray *keys = [NSArray arrayWithObjects:@"k7",@"k8",@"k9", nil];
    dic = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    NSLog(@"%@",dic);
    
}

#pragma mark 字典基本使用
void dictUse(){
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
           @"v1",@"k1",
           @"v2",@"k2",
           @"v3",@"k3",
           @"v4",@"k4",nil];
    //有多少键值对
    NSUInteger count = [dic count];
    NSLog(@"count = %ld",count);
    //取值,不能改
    NSString *obj = [dic objectForKey:@"k2"];
    NSLog(@"%@",obj);
    //将字典写入文件
    NSString *path = @"/Users/apple/Desktop/dict.xml";
    //[dic writeToFile:path atomically:YES];
    //从文件中读取字典
    NSDictionary *dict2 = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@",dict2);
}

#pragma mark 字典基本使用
void dictUse2(){
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"v1",@"k1",
                         @"v2",@"k2",
                         @"v3",@"k3",
                         @"v6",@"k4",
                         @"v7",@"k5",
                         @"v19",@"k6",
                         @"v4",@"k7",nil];
    //返回所有的key,无序的!
    NSArray *arr = [dic allKeys];
    //NSLog(@"%@",arr);
    //NSArray *arr2 = [dic allValues];
    //NSLog(@"%@",arr2);
    //key找不到是用marker代替
    arr = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k19", nil] notFoundMarker:@"not-found"];
    NSLog(@"%@",arr);
}

#pragma mark 字典的遍历
void dictFor(){
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"v1",@"k1",
                         @"v2",@"k2",
                         @"v3",@"k3",
                         @"v4",@"k4",nil];
    for(id key in dic){
        id value = [dic objectForKey:key];
        NSLog(@"%@ = %@",key,value);
    }
}

#pragma mark 字典的遍历2
void dictFor2(){
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"v1",@"k1",
                         @"v2",@"k2",
                         @"v3",@"k3",
                         @"v4",@"k4",nil];
    NSEnumerator *enumer = [dic keyEnumerator];
    id key = nil;
    while (key = [enumer nextObject]) {
        id value = [dic objectForKey:key];
        NSLog(@"%@=%@",key,value);
    }
}

#pragma mark 字典的遍历3
void dictFor3(){
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"v1",@"k1",
                         @"v2",@"k2",
                         @"v3",@"k3",
                         @"v4",@"k4",nil];
    [dic enumerateKeysAndObjectsUsingBlock:
     ^(id key, id obj, BOOL *stop) {
        NSLog(@"%@=%@",key,obj);
    }];
}

#pragma mark 内存管理
void memory(){
    Student *stu1 = [Student studentWithName:@"peter1"];
    Student *stu2 = [Student studentWithName:@"peter2"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         stu1,@"k1",
                         stu2,@"k2",nil];
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        dictFor3();
    }
    return 0;
}


//
//  Student.m
//  Foundation-NSDectionary
//
//  Created by apple on 15/6/30.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "Student.h"

@implementation Student

+(id)studentWithName:(NSString *)name{
    Student *stu = [[Student alloc]init];
    stu.name = name;
    return stu;
}

- (void)dealloc
{
    NSLog(@"%@对象被销毁了",_name);
}

@end

//
//  Student.h
//  Foundation-NSDectionary
//
//  Created by apple on 15/6/30.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic,retain)NSString *name;

+(id)studentWithName:(NSString *)name;

@end


NSMutableDictionary:

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        Student *stu1 = [Student studentWithName:@"peter1"];
        Student *stu2 = [Student studentWithName:@"peter2"];
        [dic setObject:stu1 forKey:@"k1"];
        [dic setObject:stu2 forKey:@"k2"];
        
        [dic removeAllObjects];
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值