Questions tagged [nsdictionary]

A member of Apple's Objective-C framework Cocoa. NSDictionary objects manage associations of keys and values.

A member of Apple's Objective-C () framework Cocoa. NSDictionary objects manage associations of keys and values.

An instance of NSDictionary contains a set of keys and for each key an associated value.

Both keys and values must be objects. The keys must all be different from each other. Both keys and values cannot be nil.

Resources:

4108 questions
284
votes
7 answers

for each loop in Objective-C for accessing NSMutable dictionary

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C. Suppose I have this: NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; I can set keys and values. Now, I just want to access each key and value,…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
282
votes
20 answers

How to append elements into a dictionary in Swift?

I have a simple Dictionary which is defined like: var dict : NSDictionary = [ 1 : "abc", 2 : "cde"] Now I want to add an element into this dictionary: 3 : "efg" How can I append 3 : "efg" into this existing dictionary?
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
219
votes
3 answers

Is there a way to iterate over a dictionary?

I know NSDictionaries as something where you need a key in order to get a value. But how can I iterate over all keys and values in a NSDictionary, so that I know what keys there are, and what values there are? I know there is something called a…
HelloMoon
179
votes
2 answers

Best practice? - Array/Dictionary as a Core Data Entity Attribute

I am new to Core Data. I have noticed that collection types are not available as attribute types and would like to know what the most efficient way is of storing array/dictionary type data as an attribute (e.g. the elements that make up an address…
RunLoop
  • 20,288
  • 21
  • 96
  • 151
176
votes
11 answers

adding multiple entries to a HashMap at once in one statement

I need to initialize a constant HashMap and would like to do it in one line statement. Avoiding sth like this: hashMap.put("One", new Integer(1)); // adding value into HashMap hashMap.put("Two", new Integer(2)); hashMap.put("Three", new…
user387184
  • 10,953
  • 12
  • 77
  • 147
162
votes
6 answers

How can I convert NSDictionary to NSData and vice versa?

I am sending NSString and UIImage using bluetooth. I decided to store both in a NSDictionary and then convert the dictionary to NSData. My question is how to convert NSDictionary to NSData and visa versa?
Ali
  • 10,774
  • 10
  • 56
  • 83
160
votes
12 answers

How to use NSJSONSerialization

I have a JSON string (from PHP's json_encode() that looks like this: [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}] I want to parse this into some sort of data structure for my iPhone app. I guess the best thing for me would be to have an…
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
158
votes
5 answers

How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

In my iOS 5 app, I have an NSString that contains a JSON string. I would like to deserialize that JSON string representation into a native NSDictionary object. "{\"password\" : \"1234\", \"user\" : \"andreas\"}" I tried the following…
Andreas
  • 397
  • 4
  • 18
  • 37
99
votes
6 answers

How to add to an NSDictionary

I was using a NSMutableArray and realized that using a dictionary is a lot simpler for what I am trying to achieve. I want to save a key as a NSString and a value as an int in the dictionary. How is this done? Secondly, what is the difference…
some_id
  • 29,466
  • 62
  • 182
  • 304
99
votes
2 answers

NSDictionary - Need to check whether dictionary contains key-value pair or not

I just need to ask something as follow. Suppose I am having a dictionary. NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; [xyz setValue:@"sagar" forKey:@"s"]; [xyz setValue:@"amit" forKey:@"a"]; [xyz setValue:@"nirav" forKey:@"n"]; [xyz…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
95
votes
6 answers

Using NSPredicate to filter an NSArray based on NSDictionary keys

I have an array of dictionaries. I want to filter the array based on a key. I tried this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SPORT == %@)", @"Football"]; NSArray *filteredArray = [data…
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
94
votes
3 answers

Appending NSDictionary to other NSDictionary

I have one NSDictionary and it loads up UITableView. If a user scrolls more and more, I call API and pull new data. This data is again in the form of an NSDictionary. Is it possible to add the new NSDictionary to the existing one?
slonkar
  • 4,055
  • 8
  • 39
  • 63
90
votes
3 answers

Get all keys of an NSDictionary as an NSArray

Is it possible to get all the keys from a specific NSDictionary as a seperate NSArray?
Jay
  • 913
  • 1
  • 6
  • 4
86
votes
5 answers

Converting NSString to NSDictionary / JSON

I have the following data saved as an NSString : { Key = ID; Value = { Content = 268; Type = Text; }; }, { Key = ContractTemplateId; Value = { Content = 65; Type = Text; …
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
83
votes
9 answers

NSDictionary with ordered keys

I have an NSDictionary (stored in a plist) that I'm basically using as an associative array (strings as keys and values). I want to use the array of keys as part of my application, but I'd like them to be in a specific order (not really an order…
Andy Bourassa
  • 1,838
  • 2
  • 15
  • 17
1
2 3
99 100