nsdictionary

OC中字典基本概念和常用方法(NSDictionary和NSMutableDictionary)

主宰稳场 提交于 2019-12-10 16:19:01
一:字典的基本概念 Foundation中的字典(NSDictionary和NSMutableDictionary)是由键(key)和值(value)组成的数值集合,就像我们查字典一样,通过键(key)来查找到值(value)。 key值通常是字符串对象,也可以是任意其他类型的对象,在一个字典对象中,key值必须是唯一的。 此外字典的键和值不能为空(nil),如果想在字典中加入一个空值,可以引入NSNull对象。 注意:① 字典的对象储存是没有顺序的。 ②NSMutableDictionary是NSDictionary的子类,能使用其所有方法。 ③NSMutableDictionary是NSDictionary的可修改版 ④ 字典的值(value)可以是任何类型的对象 //例如:字典的值(value)可以是数组 NSArray *arr=@[ @"1",@"2" ]; NSDictionary *dict=@{ @"name" : @"Peter", @"age" : @"18", @"subject" :arr }; 二:不可变字典( NSDictionary ) ①字典的初始化 //---简便方式 NSDictionary *dict=@{ @"name":@"Peter", @"age":@"18" }; //---dictionaryWithObjectsAndKeys

Why is NSDictionary alphabetical in memory, but not in enumeration?

柔情痞子 提交于 2019-12-10 16:17:24
问题 I understand that the right way to sort an NSDictionary is to create an array from the keys, sort the array, and then enumerate through the array and operate on the NSDictionary from there. My question is, for an NSDictionary *dict, with keys and values of strings, Why is this alphabetical: NSLog(@"%@", dict); But this is not: for (NSString *w in dict) { NSLog(@"%@", w); } Seems odd... am I doing something wrong? Thanks in advance. 回答1: That's not "in memory" -- %@ causes a message to be

How to check value null and replace it in NSDictionary

余生颓废 提交于 2019-12-10 15:39:33
问题 Now, i'm working with the NSUserDefault and the NSDictionary, i save the NSDictionary in NSUserDefault, unfortunately i can't, because NSDictionary return to Json has null value. I need check if NSDictionary has null value and replace. How? It's NSDictionary, ({ ID:11, name:21-12-2012, des:"<null>", url: [ { ID:1, name: "<null>" }, { ID:2, name:"<null>" } ] }, { ID:12, name:if i die young, des:"<null>", url: [ { ID:3, name: "<null>" }, { ID:21, name:"<null>" } ] }) 回答1: could you please check

TableView CheckMarks

情到浓时终转凉″ 提交于 2019-12-10 15:37:33
问题 I Want to Put CheckMarks in tableview when selecting array of dictionaries data. Ex:- Array contains 10 Model Names(It is Dictionary), It contains SubModels My problem is,When I select Submodel, ModelName automatically get CheckMark. Now I Put CheckMarks for different models & sub Models but how we can put checkmarks based on SubModels. My cellForRow method UITableViewCell *cell; cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITableViewCell alloc]

Cannot override initializer of NSDictionary in Swift

这一生的挚爱 提交于 2019-12-10 14:41:00
问题 I'm trying to extend the class NSDictionary in Swift to contain an NSDate that is set on init(). When I add my custom init(), I get the complier error: 'required' initializer 'init(dictionaryLiteral:)' must be provided by subclass of 'NSDictionary' However, when I add that initializer using auto-complete, I get the following error: Declarations from extensions cannot be overridden yet Is there any way to override the initializer of NSDictionary or can Swift just not handle that yet? Here's my

NSDictionary to CGPoint

坚强是说给别人听的谎言 提交于 2019-12-10 12:27:42
问题 I am working on a game, in which i am trying to fire two different types if bullets on single tap and double tap respectively. Heres what I am doing in my touch began method: - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for( UITouch *touch in touches ) { CGPoint location = [touch locationInView: [touch view]]; location = [[CCDirector sharedDirector] convertToGL: location]; NSLog(@"TOUCH LOCATION IN TOUCH BEGAN = (%f , %f)", location.x , location.y); NSUInteger tapCount

short cut technique for finding null value from Dictionary?

情到浓时终转凉″ 提交于 2019-12-10 12:15:35
问题 I have 100 key and value in nsmutabledictornary and i want to check that any value have null or not. Do you have any short function or technique? I don't want to multiple line code like check every key and value. Your answer would be appreciated. 回答1: This code will give you the set of keys which have (non)null values. You can't store actual nil values in a dictionary, so [NSNull null] is assumed. The predicate is trivially alterable to any other condition. NSDictionary *d = @{ @"a" : @"1", @

Parse Query String into a Structured NSDictionary

荒凉一梦 提交于 2019-12-10 11:33:40
问题 I have a query string: a=1&b=2&c[1]=3&c[2]=4 etc… I want a NSDictionary where a => 1 , b => 2, c => [3,4] . Notice that that the value for c is an array. It should also be able to handle something like c[1][2]=5 to make an array of arrays c => [[5]] . Of course I can do it myself by splitting on the & and = , but what about other cases such as arrays and arrays of arrays. I want a structured NSDictionary from a POST request queryString and do not want to rewrite the wheel if this already

How to set http body request efficiently?

心已入冬 提交于 2019-12-10 10:56:45
问题 in my app, I'm currently sending http requests from each viewcontroller. However, currently I'm implementing one class, that is supposed to have method for sending requests. My requests vary in number of parameters. For example, to get list of things for tableview I need to put category, subcategory, filter and 5 more parameters into request. This is what my request looks like now: NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; [request setValue:verifString

Order the Output of NSMutableDictionary like the Input

天涯浪子 提交于 2019-12-10 10:28:02
问题 how is it possbile to output the values of a dictionary in the order of input. Example: My Input: [dicValue0 setObject:@"Start Date & Time" forKey:@"START_DATETIME"]; [dicValue0 setObject:@"Specify End" forKey:@"SPECIFY_END"]; [dicValue0 setObject:@"End Date & Time" forKey:@"END_DATETIME"]; [dicValue0 setObject:@"Open End" forKey:@"END_OPEN"]; Outputs: Start Date & Time End Date & Time Specify End Open End I know how a dictionary works, but I want the output in the same order as the input! I