【问题标题】:How to access NSDictionary by key number?如何通过键号访问 NSDictionary?
【发布时间】: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

}

谢谢!

【问题讨论】:

  • NSDictionaryNSMutableDictionary 没有索引号...此外,您存储报价的方式将阻止您从同一个人获得多个报价...
  • 让你...那么你建议如何完成这项任务?
  • 所以只使用数组就可以了? @nhgrif
  • 是的。我正在发布答案。
  • 最好的方法是将所有内容放入数据库中。然后您可以按作者查询以找到该作者的多个引用,按引用反向查询以找到作者,添加带有索引号的其他列,指向照片的链接等。

标签: ios iphone objective-c ios7


【解决方案1】:

对于您要执行的操作,我建议创建一个Quote 类,该类将存储引用、“作者”以及与该人关联的图像。然后我会用这些Quote 对象的实例填充NSArrayNSMutableArray,这样你就可以随机选择一个索引。

@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 为 readonly,从而使 Quote 类不可变。


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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      - (id)randomObjectInDictionary:(NSDictionary *)dict {
          NSArray *keyArray =  [dict allKeys];
          int rand = arc4random_uniform([keyArray count]);
          return dict[keyArray[rand]];
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-05
        • 2016-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 2016-01-04
        相关资源
        最近更新 更多