How to make it so when the view loads, it displays something based on the value of an NSInteger?

♀尐吖头ヾ 提交于 2020-01-17 06:56:33

问题


I am making a quiz app that displays a saying at the end of the quiz based on the value of the NSInteger myScore. How would I code this? Any help would be helpful 'haha'. Thanks!


回答1:


Use an array that stores your witty remarks, and calculate an array index out of the score. Something like this:

NSArray *wittyNotes = [NSArray arrayWithObjects:
                       NSLocalizedString(@"Foo", nil),          // 0 - 99
                       NSLocalizedString(@"Bar", nil),          // 100 - 199
                       NSLocalizedString(@"Baz", nil),          // 200 - 299
                       NSLocalizedString(@"Over 9000", nil),    // 300 - ...
                       nil];
NSInteger index = myScore / 100;
if (index >= [wittyNotes count]) {
    index = [wittyNotes count] - 1;
}
NSString *wittyNote = [wittyNotes objectAtIndex:index];

Depending on your wishes you could make the myScore-to-index calculation more complicated.



来源:https://stackoverflow.com/questions/4987906/how-to-make-it-so-when-the-view-loads-it-displays-something-based-on-the-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!