Question

I found a code snippet here, which I wanted to use, but I can't figure out how to make it back "to string" and see if it works...

// some strings for test
NSString *cpu = [[NSString alloc] initWithFormat:@"%.0f", [_cpuSlider value]];
NSString *ram = [[NSString alloc] initWithFormat:@"%.0f", [_ramSlider value]];
NSString *hdd = [[NSString alloc] initWithFormat:@"%.0f", [_hddSlider value]];

// put them into dictionary
NSDictionary *test = [NSDictionary dictionaryWithObjectsAndKeys:cpu, @"CPU", ram, @"RAM", hdd, @"HDD", nil];
// start of the code from page above
NSMutableData *myData = [[NSMutableData alloc]init];  
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:myData];  
[archiver encodeObject:test forKey:@"MyKEY"];  
[archiver finishEncoding];  
id testJson = [NSJSONSerialization JSONObjectWithData:myData options:0 error:nil];
// end of code

// HERE I'd like know what to put in to see it as a string..e.g. { "CPU"=value; etc...}
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"MLIA" message:????? delegate:nil cancelButtonTitle:@"Zavřít" otherButtonTitles: nil];
[av show];

Thanks a lot!

Was it helpful?

Solution

NSKeyedArchiver doesn't produce JSON (AFAIK), so I don't know how that linked example could possibly work. If you want to JSON-encode a dictionary, you would use +dataWithJSONObject:options:error::

NSError *err;
NSData *json = [NSJSONSerialization dataWithJSONObject:test options:0 error:&err];
NSAssert1(json, @"Error: %@", err);

// If not using ARC, remember to (auto)release repr.
NSString *repr = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];

[[UIAlertView …] … message:repr …];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top