博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义类实现基于数组/字典Literal Syntax设置和获取数据
阅读量:7109 次
发布时间:2019-06-28

本文共 1464 字,大约阅读时间需要 4 分钟。

hot3.png

.h文件

#import 
@interface TestKVC : NSObject {        NSMutableDictionary *mDictionary;    NSMutableArray *mArray;}- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey;- (id)objectForKeyedSubscript:(id)key;- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index;- (id)objectAtIndexedSubscript:(NSUInteger)idx;@end

.m文件

#import "TestKVC.h"@implementation TestKVC- (id)init {        self = [super init];    if (self) {        mArray = [NSMutableArray array];        mDictionary = [NSMutableDictionary dictionary];    }        return self;}- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index {    [mArray insertObject:anObject atIndex:index];}- (id)objectAtIndexedSubscript:(NSUInteger)idx {    return [mArray objectAtIndex:idx];}- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey {    [mDictionary setObject:object forKey:aKey];}- (id)objectForKeyedSubscript:(id)key {    return [mDictionary objectForKey:key];}@end

使用

    TestKVC *test = [[TestKVC alloc] init];    test[@"key"] = @"Hello";    id value = test[@"key"];        [test setObject:@"World" forKeyedSubscript:@"key0"];    id value0 = [test objectForKeyedSubscript:@"key0"];        NSLog(@"%@ %@", value, value0);  // Hello World            test[0] = @"Hello";    test[1] = @"World";        id v0 = test[0];    id v1 = test[1];        NSLog(@"%@  %@", v0, v1);         // Hello World

转载于:https://my.oschina.net/petsatan/blog/424186

你可能感兴趣的文章
配置linux批量修改密码,含自动方式
查看>>
SQL Server会写入哪些注册表?
查看>>
ORACLE 归档模式
查看>>
OFFICE 2007 SP3后续补丁微软官方下载地址
查看>>
Windows Phone 联系人管理
查看>>
zabbix监控redis多实例
查看>>
启动流程
查看>>
"Volume Shadow Copy Service" error
查看>>
JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
查看>>
crontab 计划任务 linux计划任务基本
查看>>
18.存储过程--SQL
查看>>
我的友情链接
查看>>
4、xen虚拟机扩展磁盘空间一法
查看>>
Android中view和surfaceview的区别
查看>>
ISA Server签名
查看>>
金蝶结账不成功之原因分析
查看>>
2015年10月15日作业
查看>>
华为交换机查看MAC地址
查看>>
C# C/S 图片验证码功能源码
查看>>
SCVMM 2012 SP1 安装与配置指南(一)概述
查看>>