Question

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "initializer element is not constant." The compiler doesn't want to make a variable from a random number because the random number function is not constant.

How do I generate a random number and then use that same value for more than one action? (For example, to define a color and then write that value to a label?)

Code:

#import "Slider_with_IBAppDelegate.h"

float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

//^this is where I get the error: "initializer element is not constant"

@synthesize label

//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:@"%f", hue];
}

//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{
self.label5.backgroundColor = [UIColor colorWithHue:hue                        
                                             saturation:1.0                                      
                                             brightness:1.0                                       
                                                  alpha:1.0];
}

----edit------

Thanks for the suggestions. It still doesn't work for me, though, what am I doing wrong?

New code:

#import "Slider_with_IBAppDelegate.h"

float const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

//^I still get the error: "initializer element is not constant."

@synthesize label

//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:@"%f", hue];
}  
//^this is where I get the error "'hue' undeclared (first use of this function)"

//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{

hue = ((arc4random() % ((unsigned)1000 + 1))/1000.0);
/*here I get the error "assignment of read-only variable 'hue.'"  
If I insert "float" just before hue, I do not get this error, 
but it still won't compile because of the error above.*/

self.label5.backgroundColor = [UIColor colorWithHue:hue                        
                                         saturation:1.0                                         
                                         brightness:1.0                                          
                                              alpha:1.0];
}
Was it helpful?

Solution

Make it non-const and initialize it in applicationDidBecomeActive. Is there a reason it must be constant?

OTHER TIPS

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "incompatible types in initialization."

float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

That's not a function; it's an expression. I'd be surprised if you're not also getting an error here, because you can't initialize a global variable with an expression that isn't constant. As alltom.com says, you need to assign to it from applicationDidBecomeActive:.

The warning is because you've given the variable a pointer type (float *), but you're not assigning a pointer to it. Cut out the asterisk, because you're not going to put a pointer in this variable.

Xcode doesn't want to make a variable from a random number because the random number function is not constant.

Xcode doesn't care one way or the other. It's just reporting the findings of the compiler. By default, the compiler for Objective-C is GCC, but Xcode supports other compilers (and Xcode does come with one other C/Objective-C compiler: LLVM-GCC).

… I couldn't call the same value for the label.

You're not showing a label here, and you can't call a value. You can only call a function, and you don't have one in the code shown.

It gave me the error "function undefined: first use of this function" in doButton even though it was defined in applicationDidBecomeActive.

No, it wasn't. Assigning to a variable does not create a function.

In case anyone is wondering, I finally found a way to do this effectively. (I am sure this is what alltom was saying, I was just too dumb to understand.)

I declared a float and a seed in my .h file:

- (float)generate:(id)sender;
- (void)seed;

And in the implementation file, I defined the float as a random number, and I used srandom() as a random seed generator.

- (float)generate:(id)sender
{

//Generate a number between 1 and 100 inclusive
int generated;
generated = (random() % 100) + 1;

return(generated);
} 

- (void)seed {
srandom(time(NULL));
}

Then anywhere I wanted to retain a random number, I used

srandom(time(NULL));
generated1 = ((random() % 100) + 1)/100.0;

to initiate the number, and from there I was able to use generated1, generated2, hue, etc. as variables in any function I wanted (and I made sure to declare these variables as floats at the top of the file).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top