P Griep
P Griep

Reputation: 844

Java HashMap to iOS

I am a pure Java/c# developer. But now after building my native Android app, I am trying to extend my knowledge with iOS (Objective_c). Now I am at the point that there is a little difference between iOS and Android. I am trying to convert my Java HashMap to one in iOS, but found out that there is no equivalent for the iOS version.

My Java HashMap for my Android app looks like this:

Map<String, String>

Now I want the same for my iOS app. But I can't see the solution. I've did some research and found out that there is something like NSDictionary or NSMutableDictionary that can help me out.

After a little more effort I found out that a NSDictionary is immutable, but the NSMutableDictionary data structure lets you dynamically add and remove entries as necessary. So I think I could use NSMutableDictionary for my HashMap to dynamically add/remove stuff in it.

After a more research ont the internet, I found out that it is possible to do the following:

NSMutableDictionary *testHashMap = @{
    @"FirstKey" : "FirstItem",
    @"SecondKey" : "SecondItem",
    @"ThirdKey" : "ThirdItem",
    @"FourthKey" : "FourthItem"
};

Is this a good way to build a HashMap in objective_c (iOS)? Or is there something better?

Now I want to add a fifth item. How is it possible to do this? Is there something like:

testHashMap.add("FifthKey", "FifthItem");

And how do I get an item out of the testHashMap in iOS? Do I have to do something like this:

String secondItem = testHashMap("SecondKey");

Thank you for all the help!

Upvotes: 2

Views: 7472

Answers (4)

jayraj m.g.
jayraj m.g.

Reputation: 625

If you are going to store data Dynamic than use NSMutableDictionary or for Static data Use NSDictionary as follow:

NSMutableDictionary *dynamicDict=[[NSMutableDictionary alloc]init];
[dynamicDict setValue:@"FirstItem" forKey:@"FirstKey"];
[dynamicDict setValue:@"SecondItem" forKey:@"SecondKey"];
[dynamicDict setValue:@"ThirdItem" forKey:@"ThirdKey"];
[dynamicDict setValue:@"FourthItem" forKey:@"FourthKey"];

and if you are not using ARC then release that dictionary by using when dictionary have no further use.

[dynamicDict release];

Upvotes: 0

Sam Jarman
Sam Jarman

Reputation: 7377

So, another thing pointing out is the lack of Java Generics in Objective C.

So in java, you'd do like HashMap<String, String> mapping a string to a string.... which makes sense... you could do HashMap<String, MyObject> as well.

In Objective-C, specifically Cocoa's Collections (Eg NSDictionary, NSArray, NSSet) every object is of type id. I think of id as a Subclass of NSObject, as anything in Cocoa or CocoaTouch is (for most cases look for the prefix NS-).

So really, a mashup of Java and Objective-C (and this will make some people's eyes bleed) would look like this NSMutableDicionary<id, id>.

That's why you cast on the way out like Roecrew's NSString *string = (NSString*)[testHashMap objectForKey:@"FifthKey"];

If you don't know the type of object you're getting out, then be careful. But casting in objective-c works the same as java casting...so you can do that. But casting is typically indicative of bad design but that's another story for another time.

Point is, everthing is ID. Keys are usually strings, the values can be whatever you please. Good luck!

Upvotes: 0

maelswarm
maelswarm

Reputation: 1070

Too add an object with a key call the following.

  [testHashMap setObject:@"FifthObject" forKey:@"FifthKey"]; 

Too get an object convert grab an object with its key.

   NSString *string = (NSString*)[testHashMap objectForKey:@"FifthKey"];

Upvotes: 0

Oleg Fedorov
Oleg Fedorov

Reputation: 337

You can do

testHashMap[@"FifthKey"] = @"FifthItem";

And read them with

testHashMap[@"FifthKey"];

MNSMutableDictionary is a great way to store and read dynamic data. But if you have static data - just use NSDictionary. If your data is mostly static but you change it sometimes - still use NSDictionary and convert to it mutable using [dict mutableCopy], then add new items there and convert it back using [mutableDict copy].

Upvotes: 3

Related Questions