如何在NSArray中使用setValue:forKey:函数 [英] How to use setValue:forKey: function with NSArray

查看:124
本文介绍了如何在NSArray中使用setValue:forKey:函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用函数 -setValue:forKey:并使用 -valueForKey:

I'm try to use the functions -setValue:forKey: and get the value using -valueForKey:

示例:

NSArray *rootarr = [[NSArray alloc] init];
NSArray *array = [[NSArray alloc] initWithObjects:@"name", @"address", @"title", nil];
[rootarr setValue:array forKey:@"n"];
NSArray *getArr = [rootarr valueForKey:@"n"];

但是我得到的getArr数组不等于我设置的数组(数组).

But the getArr array I got is not equal the array I set (array).

能否请您告诉我我遇到了什么问题.以及使用这些功能的方式是什么?

Could you please tell me what's wrong I met. And what's the way to use these functions?

推荐答案

NSArray setValue:forKey:方法用于

NSArray's setValue:forKey: method is used for Key Value Coding, not for using an array as an associative container. You need to use NSMutableDictionary.

NSMutableDictionary *rootDict = [NSMutableDictionary dictionary];
NSArray *array = [[NSArray alloc] initWithObjects:@"name", @"address", @"title", nil];
[rootDict setValue:array forKey:@"n"];
NSArray *getArr = [rootDict valueForKey:@"n"];

这篇关于如何在NSArray中使用setValue:forKey:函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆