0

I created a single view app, added a label, an un-editable text view and a button, I have an array of strings. Really simply just want to click the button and change the string at random.

- (IBAction)viewNextPressed:(id)sender {
    NSArray *affirmationStrings = @[
                                   @"String 1 Pressed",
                                   @"String 2 Pressed",
                                   @"String 3 Pressed"
                                   ];

    //Generate a random index from our array
    int randomNIndex = arc4random() % [affirmationStrings count];

    //Display a string from our array
    self.displayAffirmationText.text = affirmationStrings[randomNIndex];

}

@end

Obviously this works fine for this example but its horribly inefficient as its generating the array each time the button is clicked. Where is the best place to store the array so its generated upon load and I can just access it when needed?

I see viewDidLoad but as a beginner I want to try to understand best practice for simple tasks. Secondly is the way I am storing strings fine for a large sample of say 500-1k+ strings?

3
  • 1
    If you want to avoid recreating the array, you could store it in an instance variable on the class which implements -viewNextPressed:. This is probably the simplest and best solution, assuming you don't have an insane number of strings and that the strings are fairly short. If there are multiple instances of the VC in question, you might consider a static instance variable. Commented Feb 21, 2016 at 13:19
  • Thanks for the reply, I am currently just using the default ViewController class. How would I implement that into ViewController.h? Commented Feb 21, 2016 at 13:39
  • 1
    It's probably best to add a property. The line of code you want is @property (nonatomic,strong) NSArray *affirmationStrings; Then setup the array in -viewDidLoad, as you do in the method, but using _affirmationStrings instead of NSArray *affirmationStrings. I strongly recommend you have a look at an introduction to Objective-C properties in order to understand exactly what it means and what it's doing, if you don't already understand properties Commented Feb 21, 2016 at 13:43

1 Answer 1

2

For a relatively small number of strings, a better option would be to:

  • add either a property or an instance variable to your class
  • initialise said property or instance variable either in your init method, or, in the case of a View Controller, in viewDidLoad.

So, in the case of an instance variable:

@implementation MyViewController
{
    NSArray *_affirmationStrings;
}

...

- (void)viewDidLoad
{
    [super viewDidLoad];
    _affirmationStrings = @[ ... list of strings ... ];
}

Then refer to it via _affirmationStrings.

In the case of a property, visible to other classes, read-only, with lazy initialization:

In .h:

@interface MyViewController : UIViewController

@property (readonly) NSArray *affirmationStrings

@end

In .m:

- (NSArray *)affirmationStrings
{
    if (!_affirmationStrings)
        _affirmationStrings = @[ ... list of strings ... ]
    return _affirmationStrings;
}

Then refer to it via self.affirmationStrings.

There are also alternatives to make it read/write (so you can set the values from another class), or visible only within the class, etc.

If you want to handle lots of strings, you probably at the very least want to move the list outside of your code, to an external file (text file, JSON, XML, plist...). You can then either load it from there at once and keep it, or load it on demand (and forget about it once you no longer need it, hence reloading it again if you need it again).

You could also store the data in a database, either via Core Data or directly with SQLite.

It really all depends on your goals/requirements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.