Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何按密钥号访问NSDictionary?_Ios_Iphone_Objective C_Ios7 - Fatal编程技术网

Ios 如何按密钥号访问NSDictionary?

Ios 如何按密钥号访问NSDictionary?,ios,iphone,objective-c,ios7,Ios,Iphone,Objective C,Ios7,我是iPhone开发的初学者,需要一些帮助 我创建了一个NSDictionary来保存一些引号,后面是引用它们的人的键 现在,我创建了一个方法来拉取一个随机引号,要使用这个方法'arc4random_uniform',我需要使用integer…并且它们我确实'返回[self.insOperationalQuotes objectForKey:这里我想要一个随机数] 通常情况下,使用数组可能会更容易,但我想使用键将其附加到引用它的人的特定照片上…或者你仍然建议使用数组并说对象为1,例如will e

我是iPhone开发的初学者,需要一些帮助

我创建了一个NSDictionary来保存一些引号,后面是引用它们的人的键

现在,我创建了一个方法来拉取一个随机引号,要使用这个方法'arc4random_uniform',我需要使用integer…并且它们我确实'返回[self.insOperationalQuotes objectForKey:这里我想要一个随机数]

通常情况下,使用数组可能会更容易,但我想使用键将其附加到引用它的人的特定照片上…或者你仍然建议使用数组并说对象为1,例如will einstein

这是我的代码:

#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

}

谢谢

对于您要做的事情,我建议创建一个Quote类,它将存储Quote、作者以及与此人关联的图像。然后,我会用这些Quote对象的实例填充NSArray或NSMutableArray,以便您可以随机选择索引

@interface Quote : NSObject

@property NSString *quote;
@property NSString *author;
@property UIImage *picture;

+(instancetype)quoteWithQuote:(NSString*)quote 
                       author:(NSString*)author 
                      picture:(UIImage*)picture;

-(instancetype)initWithQuote:(NSString*)quote  
                      author:(NSString*)author 
                     picture:(UIImage*)picture;

@end
那就填这门课吧。可以说,您甚至可能希望@propertys是只读的,从而使Quote类不可变

或者,您可以保持NSDictionary的原样,并将其与一个NSArray配对,该NSArray由您用作密钥的所有作者姓名组成。然后从名称数组中选择一个随机索引,并使用该名称作为引号的字典键。这里的问题是它有点多余,并且不能对同一作者使用多个引号。

使用allValues拉取一个值数组,并使用随机数索引到该数组中,如下所示:

- (NSString *) getRandomQuote {
    int randomNum = arc4random_uniform(self.insparationalQuotes.count);
    return [[self.insparationalQuotes allValues] objectAtIndex:randomNum];
}

我同意您真正想要的是自定义对象数组,但我想指出,可以从NSDictionary对象中选择随机条目:


NSDictionary和NSMutableDictionary没有索引号。。。此外,您存储引号的方式将防止您从同一个人那里获得多个引号…明白了…那么您建议如何完成此任务?因此,仅使用数组就可以了@恩格里菲斯。我发布了一个答案。最好的方法是把所有的东西都放到数据库中。然后,您可以按作者查询以查找该作者的多个引用,按引用反向查询以查找作者,添加带有索引号的其他列、照片链接等。
- (id)randomObjectInDictionary:(NSDictionary *)dict {
    NSArray *keyArray =  [dict allKeys];
    int rand = arc4random_uniform([keyArray count]);
    return dict[keyArray[rand]];
}