【发布时间】:2013-12-08 01:03:35
【问题描述】:
我是 iPhone 开发的初学者,我需要一些帮助。
我创建了一个 NSDictionary 来保存一些引号,然后是键(这是引用它们的人)。
现在,我创建了一个提取随机引用的方法,并且要使用此方法“arc4random_uniform”,我需要使用整数......我确实返回 [self.insperationalQuotes objectForKey:”这里我想要一个随机数"];
通常使用数组可能更容易,但我想使用密钥将其附加到引用它的人的特定照片...或者您仍然建议使用数组并在 # 处说对象比如1会爱因斯坦...
这是我的代码:
#import "NOQuotes.h"
@implementation NOQuotes
- (NSDictionary *) insparationalQuotes {
if (_insparationalQuotes == nil) {
_insparationalQuotes = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Try not to become a man of success but a man of value.",
@"ALBERT EINSTEIN",
@"What lies behind us and what lies before us are tiny matters compared to what lies within us.",
@"HENRY STANLEY HASKINS",
@"It is never too late to be what you might have been.",
@"GEORGE ELIOT",
@"All our dreams can come true–if we have the courage to pursue them.",
@"WALT DISNEY",
@"The best way to predict the future is to invent it.",
@"ALAN KAY",
@"You miss 100% of the shots you don’t take.",
@"WAYNE GRETZKY",
@"If opportunity doesn’t knock, build a door.",
@"MILTON BERLE",
@"Failure is the condiment that gives success its flavor.",
@"TRUMAN CAPOTE", nil];
}
return _insparationalQuotes;
}
- (NSString *) getRandomQuote {
int randomNum = arc4random_uniform(self.insparationalQuotes.count);
return [self.insparationalQuotes objectForKey:randomNum]; //error
}
谢谢!
【问题讨论】:
-
NSDictionary和NSMutableDictionary没有索引号...此外,您存储报价的方式将阻止您从同一个人获得多个报价... -
让你...那么你建议如何完成这项任务?
-
所以只使用数组就可以了? @nhgrif
-
是的。我正在发布答案。
-
最好的方法是将所有内容放入数据库中。然后您可以按作者查询以找到该作者的多个引用,按引用反向查询以找到作者,添加带有索引号的其他列,指向照片的链接等。
标签: ios iphone objective-c ios7