1
NSMutableDictionary *elementsMDic = [NSMutableDictionary new];
for (int i = 0 ; i < maxRssiMArray.count; i++) {

    NSString *majorStr = [NSString stringWithFormat:@"%@",[maxRssiMArray[i]valueForKey:@"major"]];
    NSString *minorStr = [NSString stringWithFormat:@"%@",[maxRssiMArray[i]valueForKey:@"minor"]];
    NSInteger rssiInt = [[maxRssiMArray[i] valueForKey:@"rssi"] intValue];

    [elementsMDic setValue:@"guest" forKey:@"user_name"];
    [elementsMDic setValue:majorStr forKey:@"major"];
    [elementsMDic setValue:minorStr forKey:@"minor"];
    [elementsMDic setValue:@(rssiInt) forKey:@"rssi"];
    [elementsMDic setValue:submitTime forKey:@"submit_time"];
    [elementsMDic setValue:HERE_BEACON_UUID forKey:@"beacon_uuid"];

    [maxRssiDicMArray addObject:elementsMDic];   
}
NSLog(@"%@",maxRssiDicMArray);

I get

{
    "beacon_uuid" = "myUUID";
    major = 65535;
    minor = 65535;
    rssi = "-95";
    "submit_time" = "2015-10-29_17:45:56";
    "user_name" = guest;
},

my question is why something get "" others get no "" I think it should be like this

        {
    "beacon_uuid" = "myUUID";
    "major" = 65535;
    "minor" = 65535;
    "rssi" = -95;
    "submit_time" = "2015-10-29_17:45:56";
    "user_name" = "guest";
},

because my Keys are all NSString , and my majorStr and minorStr all NSString,too

please tell me why and how to resolve why!!

2
  • Not a bad question, but surely that code is broken? You enumerate an array and repeatedly set the values to a single dictionary. That cannot be right. Also your use of stringWithFormat and valueForKey is just plain wrong; for example this is correct (and succinct): NSString *majorStr = maxRssiMArray[i][@"major"];. Commented Oct 29, 2015 at 10:35
  • For "single words" with only letters and that are easily understandable as string objects, it doesn't need quotes. But [object description] is not an issue for that. Commented Oct 29, 2015 at 10:48

2 Answers 2

1

What you are getting is the console output, which utilises the description property of the objects it puts out. According to the apple docs (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/#//apple_ref/occ/instp/NSDictionary/description) this is intended for readability, not for serialization of objects. so the quotes you are seeing are only there to help you read it. string values with empty spaces or other specific characters like underscore get wrapped in quotes, string values without those don't. You can't really resolve this issue, since the function is working as intended. since you didnt tell us what you are trying to do, suggesting alternatives becomes a guessing game. if you need data you can write to a file and retrieve later, you should look into property lists (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048i) or NSArchiver (https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArchiver_Class/index.html). If this is intended to be sent over a network, maybe NSJSONSerialization does what you need? https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/

Sign up to request clarification or add additional context in comments.

Comments

0

The dictionary is correct, it's just a display behavior of NSLog

If key or value contains only alphanumeric characters, the double quotes are not displayed.

NSDictionary *dict = @{@"key" : @"value"};
NSLog(@"%@", dict);

{ key = value; }

Otherwise if key or value contains spaces, underscores or other special characters

NSDictionary *dict = @{@"key 1" : @"value 1"};
NSLog(@"%@", dict);

the double quotes are displayed.

{ "key 1" = "value 1"; }

PS: Do not use valueForKey: / setValue:forKey: to get/set values of a dictionary.
The designated methods are objectForKey: / setObject:forKey:.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.