I00I
I00I

Reputation: 423

Getting a property value out of a dictionary using KVC

I have ten UIImageViews that are badges for a game in my "GoalsViewController" class they are set to hidden = YES; in Interface Builder.

I want to make them hidden = NO; when certain levels are reached in my "GameViewController" class.

I am stuck because I am not sure if the solution I have is even going to work.

What I have so far is:

In GoalsVC.h I have a

NSMutableDictionary *goalsDictionary;

and a getter method that returns goalsDictionary

-(NSMutableDictionary *)goalsDictionary;

Then in GoalsVC.m I alloc and init the goalsDictionary

goalsDictionary = [[NSMutableDictionary alloc]initWithCapacity:10];

I create an imageView

UIImageView *goalImage = [[UIImageView alloc]init]; 

goalImage.hidden = YES; 

[goalsDictionary setValue:goalImage forKey:@"PassedLevelOne"];

I repeat this same method call nine more times changing the key to different levels.

And it's now that I realize I can't get a property such as hidden from a dictionary can I? The method setValue: takes an object and the forKey: takes a string.

So is there a better way to tell GoalsViewController to set the hidden property to NO when something in my GameControllerView happens?

Upvotes: 0

Views: 867

Answers (2)

Alladinian
Alladinian

Reputation: 35706

Of course you can get a property of an object that is a value in a dictionary. You can use valueForKeyPath: method, like this:

id myProperty = [goalsDictionary valueForKeyPath:@"PassedLevelOne.hidden"];

PS. Of course in the case where the property is a primitive like BOOL you can cast the result and assign the value accordingly.

Upvotes: 1

bert
bert

Reputation: 1596

I am not sure I understand you correctly but is this what you are looking for?

UIImageView *passedLevelOneImageView = (UIImageView *)[goalsDictionary objectForKey:@"PassedLevelOne"];
passedLevelOneImageView.hidden = NO;

If not let me know and I can try again.

Upvotes: 1

Related Questions