JohnBigs
JohnBigs

Reputation: 2811

How to access NSDictionary by key number?

I'm a beginner in iPhone development and I need some help.

I created an NSDictionary to hold some quotes followed by keys (which is the person who quoted them).

Now, I'v created a method to pull a random quote, and to use this method 'arc4random_uniform' I need to use integer...and them I do 'return [self.insperationalQuotes objectForKey:"here i want a random number"];

Usually it probably would be easier to use an array but I want to use the key to attach it to a specific photo of the person who quoted it...or you still recommend to do it with array and say object at #1 will einstein for instance...

This is my code:

#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

}

Thanks!

Upvotes: 3

Views: 327

Answers (3)

nhgrif
nhgrif

Reputation: 62052

For what you're trying to do, I recommend creating a Quote class, which will store a quote, an "author", and the image associated with that person. I'd then fill an NSArray or NSMutableArray with instances of these Quote objects so you can randomly select an index.

@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

Then just fill in that class. Arguably, you may even want the @propertys to be readonly, making the Quote class immutable.


Alternatively, you can keep your NSDictionary as is, and pair it with an NSArray made up of all the author names (which you're using as keys). Then select a random index from the array of names, and use that name as the key into the dictionary for the quote. The problem here is it's a little redundant, and you can't have multiple quotes with the same author.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

Use allValues to pull an array of values, and index into that array using a random number, like this:

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

Upvotes: 1

godel9
godel9

Reputation: 7390

I agree that what you really want is an array of custom objects, but I would like to point out that it is possible to select a random entry from an NSDictionary object:

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

Upvotes: 0

Related Questions