nsdictionary

Deep combine NSDictionaries

*爱你&永不变心* 提交于 2019-11-27 20:12:25
I need to merge two NSDictionary s into one provided that if there are dictionaries within the dictionaries, they are also merged. More or less like jQuery's extend function. Alexsander Akers NSDictionary+Merge.h #import <Foundation/Foundation.h> @interface NSDictionary (Merge) + (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with: (NSDictionary *) dict2; - (NSDictionary *) dictionaryByMergingWith: (NSDictionary *) dict; @end NSDictionary+Merge.m #import "NSDictionary+Merge.h" @implementation NSDictionary (Merge) + (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with

Directly accessing nested dictionary values in Objective-C

天涯浪子 提交于 2019-11-27 19:05:12
Is there a way to directly access an inner-array of an an outer array in Objective-C? For example, a call to an external data source returns the following object: { bio = "this is the profile.bio data"; "first_name" = John; "last_name" = Doe; location = { name = "Any Town, Any State"; }; metadata = { pictures = { picture = "https://picture.mysite.com/picture.jpeg"; } } } I want to be able to access, for example, the location.name or the metadata.pictures.picture data. Dot notation, however, does not seem to work. For example: _gfbLocation = [result objectForKey:@"location.name"]; _gfbPicture =

Why is NSUserDefaults unwilling to save my NSDictionary?

耗尽温柔 提交于 2019-11-27 17:40:49
问题 I'm using KVC to serialize an NSObject and attempt to save it to NSUserDefaults , which is giving me an Attempt to insert non-property value when I try to store my NSDictionary . Following are the properties of the object in question, MyClass : @interface MyClass : NSObject @property (copy,nonatomic) NSNumber* value1; @property (copy,nonatomic) NSNumber* value2; @property (copy,nonatomic) NSString* value3; @property (copy,nonatomic) NSString* value4; @property (copy,nonatomic) NSString*

How to sort an array of dates in descending order

老子叫甜甜 提交于 2019-11-27 17:36:32
问题 I have a NSDictionary that parsed to an array one of the element is date, i tried using [startimeArray sortUsingSelector:@selector(compare:)]; (starttimeArray) is my date but it only arrange ascending. how can i sort in descending order. thanks 回答1: Sort the array by puting NO is ascending parameter: NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO]; NSArray *descriptors=[NSArray arrayWithObject: descriptor]; NSArray *reverseOrder=[dateArray

Casting a CFDictionaryRef to NSDictionary?

谁说胖子不能爱 提交于 2019-11-27 17:06:46
问题 I have the code (stripped down): CFDictionaryRef *currentListingRef; //declare currentListingRef here NSDictionary *currentListing; currentListing = (NSDictionary *) currentListingRef; And then I get the error: Cast of a non-Objective-C pointer type 'CFDictionaryRef *' (aka 'const struct __CFDictionary **') to 'NSDictionary *' is disallowed with ARC What am I doing wrong? How do I convert from a CFDictionaryRef to an NSDictionary ? 回答1: ARC changed the way bridging works. NSDictionary

From array of dictionaries, make array containing values of one key

让人想犯罪 __ 提交于 2019-11-27 16:26:44
问题 I have an array of dictionaries. I would like to extract an array with all the elements of one particular key of the dictionaries in the original array. Can this be done without enumeration? 回答1: Yes, use the NSArray -valueForKey: method. NSArray *extracted = [sourceArray valueForKey:@"a key"]; 回答2: Yes, just use Key-Value Coding to ask for the values of the key: NSArray* names = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"Joe",@"firstname", @"Bloggs",@"surname",

Parsing a JSON array into a NSDictionary

夙愿已清 提交于 2019-11-27 16:22:14
问题 I'm working with the Weather Underground API to make an app and I've hit a snag while parsing the block relating to severe alerts. The JSON uses key-value pairs that have sub key value pairs -- which haven't been a problem for me, as I can make subsequent NSDictionaries out of those -- but the entry for severe alerts has proven problematic. See below: "alerts": [ { "type": "WAT", "description": "Flash Flood Watch", "date": "3:13 PM EDT on April 28, 2012", "date_epoch": "1335640380", "expires"

Keeping dictionary keys of JSON Object in order in Objective C

筅森魡賤 提交于 2019-11-27 16:18:55
I'm using the JSON-FRAMEWORK in objective C to parse a JSON object. When I call [jsonString JSONValue], I get back a dictionary, but the keys aren't in the same order as the JSON Object I'm parsing. Is there anyway to keep this order the same? In JSON objects, by definition , the order of the key-value pairs is not meaningful. The specification allows a JSON producer to permute them any way it want, even at random -- and does not require a parser to preserve the ordering. RFC 4627 says: An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value

Send Nested JSON using AFNetworking

懵懂的女人 提交于 2019-11-27 16:10:21
问题 To send registration data to server, I am using JSON is in following form: {"regData": { "City":"Some City", "Country":"Some Country", "Email_Id":"abc@gmail.com", "MobileNumber":"+00xxxxxxxxxx", "UserName":"Name Of user" } } Here is how am sending. NSURL * url = [[NSURL alloc] initWithString:registerUrlString]; AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; httpClient.parameterEncoding = AFJSONParameterEncoding; [[AFNetworkActivityIndicatorManager sharedManager]

Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

試著忘記壹切 提交于 2019-11-27 15:50:48
问题 I want to use valueForKeyPath on my NSDictionary , but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key. I'm having problems trying to create the key path as I'm getting a format exception, even when trying to escape the @ symbol: This works fine: [[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"] However none of these work: [dict valueForKeyPath:@"key1.@specialKey.key3"] [dict