Match String to ABPropertyID - iphone

I'm working now with the AddressBook and I need to convert(match) the String representation of ABPropertyID to ABPropertyID:
#"kABPersonEmailProperty" -> ABPropertyID kABPersonEmailProperty;
Can I do it flexible without using ifs?

Can I do it flexible without using ifs?
Not really. The easiest way is probably to manually prepare an NSDictionary in code that maps between the two. There is no way to do this entirely automatically because the names of the constants are not part of the compiled program.
Example:
NSDictionary *mapping = [NSDictionary dictionaryWithObjectsAndKeys:
#"kABPersonEmailProperty", [NSNumber numberWithInteger:kABPersonEmailProperty],
#"kABPersonEmailProperty", [NSNumber numberWithInteger:kABPersonBirthdayProperty],
nil];

Related

how to add arrays in dictionary based on particular key

I am making an app. where i want to add vendor name and it's device name..
I am using a dictionary where vendor name is key and device name are values..
Here is my code
[appDelegate.arrOfDevice addObject:txtDeviceName.text];
[appDelegate.dictOfDetails setObject:appDelegate.arrOfDevice forKey:txtVendorName.text ];
Here arrOfDevice is an array(declared in appdelegate) which is having all devices which are added..
I want to add devices based on particular keys...
I know, i am doing something wrong,
I am pushing the array as values in dictionary so it will store all the device names for each key.. but please help me...So that i could store device names based on particular key...
If u are not able to understand please fell free to get clarification of my question...
If you are adding key for the first time then you need to do this as shown below
if(flag==1) {
NSMutableArray *tmpArr=[[NSMutableArray alloc]init];
[tmpArr addObject:txtDeviceName.text];
[appDelegate.dictOfDetails setObject:tmpArr forKey:[txtVendorName.text uppercaseString]];
}
and if the key is present you just need to create a reference for an array and add object to the dictionary
else if(flag==0) {
NSMutableArray *arrDevice=[appDelegate.dictOfDetails objectForKey:[txtVendorName.text uppercaseString]];
[arrDevice addObject:txtDeviceName.text] ;
}
NSDictionary *dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"Nit",[NSNumber numberWithInt:3],nil]
forKeys:[NSArray arrayWithObjects:#"Name",#"num",nil]];
>>Edited
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
#"value1", #"key1", #"value2", #"key2", nil];
also seem +dictionaryWithObjects:forKeys:
Hope, this will help you...

JSON Sort String

framework.
Hi I'm using json-framework.
How to get sorted string when using JSONRepresentation method?
Here is my code:
NSMutableDictionary *tUserIDParam = [NSMutableDictionary dictionary];
[tUserIDParam setObject:#"UserID" forKey:#"#key"];
[tUserIDParam setObject:#"string" forKey:#"#type"];
[tUserIDParam setObject:#"mark" forKey:#"#text"];
NSMutableDictionary *tPasswordParam = [NSMutableDictionary dictionary];
[tPasswordParam setObject:#"Password" forKey:#"#key"];
[tPasswordParam setObject:#"string" forKey:#"#type"];
[tPasswordParam setObject:#"123456" forKey:#"#text"];
NSArray *tParams = [NSArray arrayWithObjects:tUserIDParam,tPasswordParam, nil];
NSDictionary *tParam = [NSDictionary dictionaryWithObject:tParams forKey:#"param"];
NSMutableDictionary *tRequests = [NSMutableDictionary dictionary];
[tRequests setObject:[NSNull null] forKey:#"key"];
[tRequests setObject:[NSNull null] forKey:#"basic"];
[tRequests setObject:tParam forKey:#"conditions"];
NSDictionary *tRequest = [NSDictionary dictionaryWithObject:tRequests forKey:#"request"];
NSString *tJSONStrFromDict = [tRequest JSONRepresentation];
I want to get JSON string like:
{"request":{"key":null,"basic":null,"conditions":{"param":[{"#key":"UserID","#type":"string","#text":"mark"},{"#key":"Password","#type":"string","#text":"123456"}]}}}
But I got this string from above code:
{"request":{"basic":null,"conditions":
{"param":[{"#text":"mark","#key":"UserID","#type":"string"},{"#text":"123456","#key":"Password","#type":"string"}]},"key":null}}
I try to use SBJSONWriter sortKeys property,it does not work...
Have any way to solve my problem?
Thanks!
NSMutableDictionary instance do not preserve the order of elements, neither in alphabeticallz order nor in the order they were inserted. This is independent of JSON.
Furthermore, JSON's data model for object doesn't guarantee order either. If you want a certain order, you'll have to use JSON arrays for the ordered parts.
BTW.: The effect of SBJSONWriter sortKeys is to output the object elements (or properties) alphabetically sorted by the key name. This is what you get.

YAJL - JSON on iOS/iPhone

In my iPhone app, I am trying to use the JSON library (YAJL) to create a JSON string that looks like the following format:
{"user":
{"name":"Jon", "username":"jon22", "password":"passw#rd", "email":"jon22#example.com"}
}
But I can't figure out the YAJL methods to create this.
I have tried the following:
NSArray *params = [NSArray arrayWithObjects:#"Jon", #"jon22", #"passw#rd", #"jon22#gmail.com", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"username", #"password", #"email", nil];
NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:params forKeys:keys];
NSString *JSONString = [userDictionary yajl_JSONString];
However, the returned string is not wrapped in the outside "user".
How can I use YAJL to create this json string? Does anyone have experience with this???
Many thanks,
Brett
I don't use YAJL, but SBJSON, which is the one Apple uses in iOS too...
Anyway, the library is behaving correctly: you're not creating an "user" dictionary!
You need to do something like:
NSDictionary *serialize = [NSDictionary dictionaryWithObject:userDictionary forKey:#"user"];
And then "JSON-ize" this one.
This is because when you call -yajl_JSONString on userDictionary, you are using userDictionary as your "root object". If you want to wrap this inside another dictionary, you need to explicitly do so.
I highly recommend that you check out JSONKit https://github.com/johnezang/JSONKit I have used numerous JSON parsers and have found it to be the easiest.
Also the documentation is awesome.

How to read all NSDictionary objects for a key to an array

I have a dictionary "playerDict" that reads data from a plist where there is names (myKey) with nine associated objects to each key.
I am trying to read all objects for a specific key (myKeys) into an NSMutableArray (theObjects). I have read the class reference and search internet but cannot figure our this, probably very simple, problem.
Among all other test i have done I have tried the following but that returns the key into theObjects and not the objects.
theObjects = [playerDict objectForKey:myKeys];
Anyone that could give a hint?
Here is the code that created the dict, i stripped it:
NSArray *objs = [NSArray arrayWithObjects:[NSNumber numberWithBool:playerObject.diffHard],[NSNumber numberWithBool:playerObject.diffMedium],
[NSNumber numberWithBool:playerObject.diffEasy],[NSNumber numberWithBool:playerObject.currentGame],
[NSNumber numberWithInt:playerObject.currentGameQuestion],[NSNumber numberWithInt:playerObject.currentGameRightAnswer],
[NSNumber numberWithInt:playerObject.currentGameType],[NSNumber numberWithInt:playerObject.nrOfType0Games],
[NSNumber numberWithInt:playerObject.type0Result], nil];
NSDictionary *newPlayerDict = [NSDictionary dictionaryWithObjectsAndKeys:objs, keyString, nil];
Try valueForKey:
You can only store one item per key in an NSDictionary. If you need story multiple items for the same key, you need to first add each of the items to an NSArray (or NSSet) that you instead then set as an object in your dictionary.
If might be useful if you posted the code that creates the dictionary.
Update: It looks like you are already doing this. So:
NSArray *myObjs=[playerDict objectForKey:keyString];
will get you your array. And this:
BOOL diffHard=[[myObjs objectAtIndex:0] boolValue];
BOOL diffMedium=[[myObjs objectAtIndex:1] boolValue];
Will get you the value you stored in the first and second objects of the array. Repeat it for the rest.

Algorithm: array of arrays in Cocoa Touch (perhaps using NSCountedSet)

This one is a bit tedious in as far as explaining, so here goes. I'm essentially populating a tableView on the iPhone with multiple sections, and potentially multiple rows per section. To my understanding, it's best to have an array of arrays so that you can simply determine how many sections one has by sending a message to the top level array of count, then for rows per section, doing the same for the inner array(s). My data is in the form of a dictionary. One of the key/value pairs in the dictionary determines where it will be displayed on the tableView. An example is the following:
{
name: "bob",
location: 3
}
{
name: "jane",
location: 50
}
{
name: "chris",
location: 3
}
In this case I'd have an array with two subarrays. The first subarray would have two dictionaries containing bob and chris since they're both part of location 3. The second subarray would contain jane, since she is in location 50. What's my best bet in Cocoa populate this data structure? A hash table in C would probably do the trick, but I'd rather use the classes available in Cocoa.
Thanks and please let me know if I need to further clarify.
The following code works: (edit: added my initialization code)
NSArray * arrayOfRecords = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
#"bob", #"name",
[NSNumber numberWithInt:3], #"location", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
#"jane", #"name",
[NSNumber numberWithInt:50], #"location", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
#"chris", #"name",
[NSNumber numberWithInt:3], #"location", nil],
nil];
NSMutableDictionary * sections = [NSMutableDictionary dictionary];
for (NSDictionary * record in arrayOfRecords)
{
id key = [record valueForKey:#"location"];
NSMutableArray * rows = [sections objectForKey:key];
if (rows == nil)
{
[sections setObject:[NSMutableArray arrayWithObject:record] forKey:key];
}
else
{
[rows addObject:record];
}
}
NSArray * sortedKeys = [[sections allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSArray * sortedSections = [sections objectsForKeys:sortedKeys notFoundMarker:#""];
NSLog(#"%#", sortedSections);
And NSDictionary is a hash table.
In Cocoa, it's best to use model objects rather than primitive collections, especially when using Bindings. The dictionaries should certainly be model object, and you may want to turn your inner arrays into model objects as well. (The outer array should stay an array.)
Cocoa Touch doesn't have Bindings, but I find (in Cocoa, as I don't program my iPhone) that model objects make things easier to think about and work with. I recommend you make model objects anyway.
(“Model” refers to Cocoa's “Model-View-Controller” pattern, in which Cocoa provides view and controller objects and you provide the model.)