-2

This is my code but it does not log any keys or values

NSDictionary *d = @{
      @"foo": @"bar",
      @"abc": @"def"
    };

    for (NSString* key in d.allKeys) {
        NSString *value = (NSString *)[d objectForKey:key];
        NSLog(@"areacodedisplay: value = %@",value);
        NSLog(@"areacodedisplay: key = %@",key);
    }

How to get key and value from an NSDictionary?

totalitarian
  • 3,606
  • 6
  • 32
  • 55

3 Answers3

2

look here: for each loop in objective c for accessing NSMutable dictionary

for (NSString* key in d) {
    id value = [d objectForKey:key];
    NSLog(@"areacodedisplay: value = %@",value);
    NSLog(@"areacodedisplay: key = %@",key);
}
Community
  • 1
  • 1
Roman Roba
  • 405
  • 5
  • 11
  • 2
    @totalitarian - Uh, maybe it's those things called "key" and "value". – Hot Licks Dec 14 '14 at 15:25
  • This is essentially identical to the OP's code. Iterating an `NSDictionary` (as in this answer) is the same as iterating the keys (as in the OP's code). So I'm confused as to why this answer was accepted. If this works then the OP's code must also work. – rmaddy Dec 14 '14 at 17:23
  • @rmaddy - The whole question deserves to be closed as "can no longer be reproduced". – Hot Licks Dec 14 '14 at 19:54
2

This code works:

NSDictionary *d = @{
    @"foo": @"bar"
};
    
for (NSString* key in d.allKeys)
{
    NSString *value = [d objectForKey:key];
    NSLog(@" value = %@",value);
    NSLog(@"key = %@",key);
}
Dada
  • 6,313
  • 7
  • 24
  • 43
sunil singh
  • 111
  • 10
-1

var Dict1 = NSMutuabledictionary() dict1.setValue(lbl1.text ?? "", forKey: "name") dict1.setValue(lbl2.text ?? "", forKey: "email") dict1.setValue( lbl3.text ?? "", forKey: "phone") dict1.setValue( lbl4.text ?? "", forKey: "date") dict1.setValue(lbl15.text ?? "", forKey: "password") dict1.setValue( lbl6.text ?? "", forKey: "confirm")

  • 1
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch May 13 '22 at 06:01