Created
February 14, 2012 19:49
-
-
Save ymirpl/1829648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** One More Application | |
Imagine that you were creating a lottery application (or more likely, a lottery view within another application). You might have 10 views, each with a number, and you highlight each image as a number is drawn. In this case, you’d probably have ten instance variables, maybe named numberView1, numberView2, all the way to numberView10. To light up the views, you could have a large if…else or switch block, comparing the new value to all the values 1–10: | |
**/ | |
int nextNum = arc4random() % 10 } 1; // Random number generator, 1–10 | |
switch (nextNum) { | |
case 1: | |
// Highlight | |
break; | |
// etc. | |
} | |
// Knowing KVC though, there is a more elegant solution: | |
[[self valueForKey:[NSString stringWithFormat:@“numberView%d”, nextNum]] highlight]; // Assume highlight method exists and works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment