NSDictionary添加NULL键值 [英] NSDictionary add NULL keyvalue

查看:65
本文介绍了NSDictionary添加NULL键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一个NSDictionary,该NSDictionary正在提供要使用的方法数据,但是keys数组中的某些键值将不会总是被值填充.比如说我有18个键(我这样做).在一次调用中,我可能只使用5个,而在另一个调用中仅使用6个,甚至在代码中的某些时候使用全部18个.

So I am creating a NSDictionary that is feeding a method data to be used, but some of the key values in the keys array will not always be filled with values.. Say I have 18 keys (which I do).. I might only use 5 during one call but 6 in the other or even all 18 at some points in my code.

我想知道的是如何将NULL值添加到这些键中,以便以后在查看键值对时可以看到我需要担心的键以及哪些可以忘记的键?因此,问题是,如果我不希望对键添加任何内容/空值,该怎么做而又不会导致错误或不认为其为数组nil的末尾.

What I would like to know is how can I add NULL values into those keys so later on when i look through the keyvalue pairs I can see which keys i need to worry about and which I can forget? so the question is, if i want to add nothing/null to a key how do I do that without causing errors or thinking its the end of the array nil..

这是我目前拥有的,但是当我尝试阅读NSDicionary时,它给我带来了错误的exe错误

This is what I have currently but its giving me a bad exe error as soon as I try to read the NSDicionary

NSDictionary * sendSeriesDictionary = [[NSDictionary alloc] init];

NSDictionary *sendSeriesDictionary = [[NSDictionary alloc] init];

NSArray *keys = [NSArray arrayWithObjects:@"Code", @"MaID", @"MoID", @"SuID",@"SMID",@"Ye",@"KID",@"KBlID",@"KBk",@"CaID",@"Caer",@"Loe",@"Kepe",@"KTGroup",@"Cesc",@"LID",@"IN",@"LaID", nil];


NSArray *objects = [NSArray arrayWithObjects: NULL, manufactureIdString, modelIdString, subModelIdString, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, nil];

sendSeriesDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

只是为了让您知道键名将被更改,这仅是为了测试/示范如何执行此操作.

just to let you know the key names will be changed this is just for testing/example out how to do this.

任何帮助将不胜感激.

推荐答案

The NSNull class is designed for this. You just have to make sure that whatever is going to be using the collection checks for NSNull values.

NSArray *objects = [NSArray arrayWithObjects: [NSNull null], manufactureIdString, modelIdString, subModelIdString, [NSNull null], ..., nil];

这篇关于NSDictionary添加NULL键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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