-2

Im trying to display a word picked at random on an action from my array

Ive looked at Randomize words but still not getting it to work.

My label text is _answer

in my viewDidLoad:

NSArray *words = [NSArray arrayWithObjects: @"Blue", @"Green", @"Red", nil ];
NSString *str=[words objectAtIndex:arc4random()%[words count]];

under my action method:

 [_answer setText:[NSString stringWithFormat:@"%d", arc4random()%[words count]];

I get an unused string error for str

And in my action method I have an error use of undeclared identifier "words" but its in the viewDidLoad

Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • What it must be like to never make a mistake (referring to the above comment) – JSA986 Jan 01 '13 at 20:42
  • 1
    Let's be constructive here. Everyone has to learn somewhere, not all of us were born speaking code. but @JSA986 I strongly recommend you get an intro to/beginners book for Objective-C and read it thoroughly. It will hopefully cover core concepts like scope, public vs private, static, protocols, and much, much more that would be very helpful to you. – Levi Jan 01 '13 at 20:46
  • Thanks Levi for your advice – JSA986 Jan 01 '13 at 20:48

3 Answers3

4

Scope. The variables you created in your viewDidLoad method (words, str) are only valid inside that method (that is their scope). If you want to use them in another method, such as your 'action' method, you need to declare them in the class scope as a member variable/property.

As an example, in your .h file:

@interface ExampleViewController : UIViewController
    @property(nonatomic) NSString *answer;
    // ... your other stuff ...
@end

In your .m file:

@synthesize answer;
- (void)viewDidLoad
{
    NSArray *words = [NSArray arrayWithObjects: @"Blue", @"Green", @"Red", nil ];
    self.answer = [words objectAtIndex:arc4random()%[words count]];
    [super viewDidLoad];
}

Finally, still in .m, your action:

[_answer setText:self.answer];

You can also just decalre it as a member variable (not a property).

Levi
  • 2,103
  • 14
  • 9
0

Variables declared in a method are local to that method. To share variables amongst methods in the same class make them instance variable, preferably by making them @propertys.

You get unused string error for str because it is not used after it is set.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

assign the object str to your label. [words count] contains the count of the array not the value.

Make the str object global and then use that object to assign the value to the label.

Abhishek
  • 149
  • 5
  • 1
    please don't use global variables. they make me shudder every time i see them. Objective-C is an object oriented language, and globals, while rarely necessary, help break the paradigm. – Levi Jan 01 '13 at 20:34
  • Globals are evil, avoid if at all possible. In this case use an ivar. – zaph Jan 01 '13 at 20:35
  • then create a property of your string and use that to assign the label – Abhishek Jan 01 '13 at 20:37