-2

I'm a noob to objective-c and I'm trying to create a dictionary with

key:string and value:<string, string>.  
user235236
  • 447
  • 1
  • 5
  • 16
  • 1
    What's your question? – rmaddy Sep 11 '15 at 17:27
  • BTW - use `setObject:forKey:`, not `setValue:forKey:`. – rmaddy Sep 11 '15 at 17:27
  • My question was basically looking for a tuple class, I guess... – user235236 Sep 11 '15 at 17:32
  • Your question isn't clear. You claim you want a dictionary with a string key and a string value. And that's what your code does (other than using the wrong method). So again, what is your question about dictionary? What's wrong with the code you posted? – rmaddy Sep 11 '15 at 17:34
  • I said I want to create a dictionary with key:string and value:. The value should be a tuple of – user235236 Sep 11 '15 at 17:47
  • That's the confusion. Your "clarification" in your question and your code both show otherwise. You can use an array of strings for your values as one option. – rmaddy Sep 11 '15 at 17:48

2 Answers2

3

The best way to create a dictionary is to use the NSDictionary class.

NSDictionary *dict = @{@"key0": @"val0",
                       @"key1": @"val1"};

There is also a mutable variation called NSMutableDictionary that you can append.

To access elements in your dictionary, you subscript like so

NSString *val = dict[@"key0"];

The dictionary can store any sort of NSObject subclass as values.

EDIT:

I missed the part where you want to store a "tuple". You can use an array to do this

NSDictionary *dict = @{@"key0": @[@"val0", @"val1"],
                       @"key1": @[@"val2", @"val3"]};

EDIT2

NSMutableDictionary *dict = [@{@"key0": @[@"val0", @"val1"],
                           @"key1": @[@"val2", @"val3"]} mutableCopy];

dict[@"key2"] = @[@"val10", @"val11"];
Chris
  • 7,270
  • 19
  • 66
  • 110
0

You may create custom pair class

@interface Pair : NSObject 
  @property (nonatomic, strong) NSString *first;
  @property (nonatomic, strong) NSString *second;
@end
user996142
  • 2,753
  • 3
  • 29
  • 48