nsdictionary

Object keys with NSMutableDictionary (Objective-C)

故事扮演 提交于 2019-12-04 04:31:59
I want to store a bunch of key value pairs, with the key being my own object (ObjectA) that inherits from NSObject, and the value being an int. I am trying to use an NSMutableDictionary. I understand that you can only store object types in the dictionary, so I have the following: id value = [NSNumber numberWithInt:my_number]; [[self dictionary] setObject:value forKey:myObjectA]; Now that gives me an error, saying -[ObjectA copyWithZone:]: unrecognized selector sent to instance which is fine, I understand that object keys need to implement the NSCopying protocol. However I then read that you

Keys in NSDictionary can be duplicated?

大兔子大兔子 提交于 2019-12-04 03:42:11
From what I have read, keys in dictionaries are unique. Consider this code: NSMutableDictionary *mydic = [NSMutableDictionary dictionary]; [mydic setObject:@"value1" forKey:@"key1"]; [mydic setObject:@"value1" forKey:@"key1"]; [mydic setObject:@"value1" forKey:@"key1"]; Why can I run this without any error? What should I do to avoid duplicate keys? Yes keys are unique. Calling -setObject:forKey: with an existing key overrides the old value — it sets values, not adds values. You can check that: [mydict setObject:@"1" forKey:@"key1"]; [mydict setObject:@"2" forKey:@"key1"]; NSLog(@"%@", mydict);

How to get object for key from NSDictionary?

大城市里の小女人 提交于 2019-12-04 03:08:05
In dictionary named dict the key value pair is: "alba\U2019s window" = "A model in westindies."; And to send objectForKey: I am getting the string like "alba's window". When I send like following: [dict objectForKey:alba's window]; I am not getting anything, it is showing null. For starters, you might want to make that an actual string, like so: [dict objectForKey:@"alba's window"]; Note also that \U2019 is not ' ; but ’ , which is a different character entirely. And more generally, sticking to [a-zA-Z0-9]+ for dictionary keys is probably a good idea, unless you are inserting and retrieving

Pesky new lines and whitespace in XML reader class

我的未来我决定 提交于 2019-12-04 02:58:06
问题 I'm using a class written by a blogger (http://troybrant.net/blog/) that takes an XML string and spits out a NSDictionary. It's beautiful...works perfectly, except I end up with a weird configuration of newlines and white space at the beginning of many element values. I haven't been able to figure out why. I'm posting the class here so I can get a second pair of eyes...see if anyone notices anything suspect in the method he is using. // // XMLReader.m // #import "XMLReader.h" NSString *const

[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

瘦欲@ 提交于 2019-12-04 02:50:33
问题 I am trying to add "dateTime" to My dictionary as defined follows: Symptom Ranking: { 5111ef19253b4a9150000000 = 1; 5111f029253b4add4e000000 = 1; 5111f036253b4a123d000001 = 1; 5111f045253b4a404f000000 = 1; } NSLog(@"date selected: %@", [[self.datePicker date] description]) [self.results setObject:[[self.datePicker date] description] forKey:@"dateTime"]; App crashes and I get this: Symptom Tracker[43134:c07] -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7603990

Objective C - Change all attributes in NSAttributedString?

雨燕双飞 提交于 2019-12-04 02:49:11
[attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) { NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes]; [mutableAttributes setObject:[NSNumber numberWithInt:1] forKey:@"NSUnderline"]; attributes = mutableAttributes; }]; I am trying to loop through all attributed and add NSUnderline to them. when debugging it seems like NSUnderline is added to the dictionary, but when i loop for the second time they are removed. Am I doing anything

NSDictionaries and Nested JSON

坚强是说给别人听的谎言 提交于 2019-12-04 02:19:52
问题 I am able to create a NSDictionary from a JSON file no problem, what I need to know is how to do nested JSON strings?? See Example: { "data": [ { "id": "270639882984792_306265986160413", "from": { "category": "Non-profit organization", "name": "My Facebook Page", "id": "270639882984792" }, So this is part of a massive JSON file, but how do I set it up, so that I can call the "name" key from the "from" key - I know how to call the "id" key from the "data" key, but the one I want is a level

When is NSCopying needed?

好久不见. 提交于 2019-12-04 02:13:26
I know it's needed if your object will be used as a key in an NSDictionary. Are there any other times like this that NSCopying is required? If I think I don't need my model objects to conform to NSCopying, am I probably wrong? When it's being passed to a copy property or any other method that is documented as copying its argument. Think of the NSCopying protocol as the objective-C version of cloning routines. If a caller was to clone your object, what is the behavior you would want? If your object is solely composed of primitive types, then you don't need to worry about this. But if you have

How to convert NSDictionary to custom object

不问归期 提交于 2019-12-04 01:26:57
I have a json object: @interface Order : NSObject @property (nonatomic, retain) NSString *OrderId; @property (nonatomic, retain) NSString *Title; @property (nonatomic, retain) NSString *Weight; - (NSMutableDictionary *)toNSDictionary; ... - (NSMutableDictionary *)toNSDictionary { NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; [dictionary setValue:self.OrderId forKey:@"OrderId"]; [dictionary setValue:self.Title forKey:@"Title"]; [dictionary setValue:self.Weight forKey:@"Weight"]; return dictionary; } In string this is: { "Title" : "test", "Weight" : "32", "OrderId" : "55"

How to get values from NSDictionaries inside an NSArray

我的未来我决定 提交于 2019-12-04 00:42:39
I'd like to know if it's possible to get a values from a desired key inside an NSDictionary that is in NSArray . Example: NSArray *array = @[ @{@"title" : @"title 1", @"description" : @"description 1"}, @{@"title" : @"title 2", @"description" : @"description 2"}, @{@"title" : @"title 3", @"description" : @"description 3"} ]; I'd like to get (without creating a new NSMutableArray ) all the titles inside my NSArray . Maybe I'm doing something wrong and my approach is bad altogether. I know I could create a new class and have 2 properties (title, desc), but is there a way without creating a class