Skip to main content
Commonmark migration
Source Link

#ViewController.h

ViewController.h

#ViewController.m

ViewController.m

#MBFQuotes.h / MBFQuotes.m

MBFQuotes.h / MBFQuotes.m

#ViewController.h

#ViewController.m

#MBFQuotes.h / MBFQuotes.m

ViewController.h

ViewController.m

MBFQuotes.h / MBFQuotes.m

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Our four IBAction methods look quite similar, don't they? Why don't we combine them into one methodcombine them into one method.

Our four IBAction methods look quite similar, don't they? Why don't we combine them into one method.

Our four IBAction methods look quite similar, don't they? Why don't we combine them into one method.

added 180 characters in body
Source Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129

EDIT: All right, so you can't actually have const NSArray objects as far as I can tell. The answer has been edited to the next best solution:

// MBFQuotes.m

#import "MBFQuotes.h"

const NSArray *generalQuotes = @[@"g1",NSString @"g2",* @"g3",randomGeneral() @"g4"];{
const NSArray *lifeQuotes = @[@"l1", @"l2"];
conststatic NSArray *politicsQuotes*generalQuotes = @[@"p1"@[@"g1", @"p2"];
const NSArray *smartQuotes = @[@"s1"@"g2", @"s2"];

NSString * randomGeneral()@"g3", {@"g4"];
    int randomGeneral = arc4random_uniform([generalQuotes count]);
    return [generalQuotes objectAtIndex:randomGeneral];
}

NSString * randomLife() {
    static NSArray *lifeQuotes = @[@"l1", @"l2"];
    int randomLife = arc4random_uniform([lifeQuotes count]);
    return [lifeQuotes objectAtIndex:randomLife];
}

NSString * randomPolitical() {
    static NSArray *politicsQuotes = @[@"p1", @"p2"];
    int randomPolitical = arc4random_uniform([politicsQuotes count]);
    return [politicsQuotes objectAtIndex:randomPolitical];
}

NSString * randomSmart() {
    static NSArray *smartQuotes = @[@"s1", @"s2"];
    int randomSmart = arc4random_uniform([smartQuotes count]);
    return [smartQuotes objectAtIndex:randomSmart];
}
// MBFQuotes.m

#import "MBFQuotes.h"

const NSArray *generalQuotes = @[@"g1", @"g2", @"g3", @"g4"];
const NSArray *lifeQuotes = @[@"l1", @"l2"];
const NSArray *politicsQuotes = @[@"p1", @"p2"];
const NSArray *smartQuotes = @[@"s1", @"s2"];

NSString * randomGeneral() {
    int randomGeneral = arc4random_uniform([generalQuotes count]);
    return [generalQuotes objectAtIndex:randomGeneral];
}

NSString * randomLife() {
    int randomLife = arc4random_uniform([lifeQuotes count]);
    return [lifeQuotes objectAtIndex:randomLife];
}

NSString * randomPolitical() {
    int randomPolitical = arc4random_uniform([politicsQuotes count]);
    return [politicsQuotes objectAtIndex:randomPolitical];
}

NSString * randomSmart() {
    int randomSmart = arc4random_uniform([smartQuotes count]);
    return [smartQuotes objectAtIndex:randomSmart];
}

EDIT: All right, so you can't actually have const NSArray objects as far as I can tell. The answer has been edited to the next best solution:

// MBFQuotes.m

#import "MBFQuotes.h"

NSString * randomGeneral() {
    static NSArray *generalQuotes = @[@"g1", @"g2", @"g3", @"g4"];
    int randomGeneral = arc4random_uniform([generalQuotes count]);
    return [generalQuotes objectAtIndex:randomGeneral];
}

NSString * randomLife() {
    static NSArray *lifeQuotes = @[@"l1", @"l2"];
    int randomLife = arc4random_uniform([lifeQuotes count]);
    return [lifeQuotes objectAtIndex:randomLife];
}

NSString * randomPolitical() {
    static NSArray *politicsQuotes = @[@"p1", @"p2"];
    int randomPolitical = arc4random_uniform([politicsQuotes count]);
    return [politicsQuotes objectAtIndex:randomPolitical];
}

NSString * randomSmart() {
    static NSArray *smartQuotes = @[@"s1", @"s2"];
    int randomSmart = arc4random_uniform([smartQuotes count]);
    return [smartQuotes objectAtIndex:randomSmart];
}
Source Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129
Loading