开发者

Method returns 0 instead of the number I assigned to the variable

I'm trying to get the basics of iOS programming down. I have an app that shows a random number when I click a button.. At least, that's what I wanted to make. However, it doesn't seem to be working out.

I have the following basic method which should set the text of myLabel to the return value of generateRandomNumber. However, it always returns 0. I think the syntax I'm using here is correct since it works for the commented parts:

-(IBAction)myBtnPressed:(UIButton *)sender
{
    //[myLabel setText:@"test"];
    //[myLabel setText:[NSString stringWithFormat:@"%g / 15", 3.14]];
    [myLabel setText:[NSString stringWithFormat:@"%g / 15", [myModel generateRandomNumber]]];
}

The last line sets the label to display 0/1开发者_开发知识库5. However, in my model, I have the following code ('static' for now):

-(double)generateRandomNumber
{
    randomNumber = 1.34;
    return randomNumber;
}

It doesn't return the 1.34 and I don't understand why it doesn't. Can someone clear this up?

Update This is the code for my viewcontroller.m file:

#import "myViewController.h"

@implementation myViewController

    -(MyModel *)myModel
    {
        if (! myModel) {
            myModel = [[MyModel alloc] init];
        }

        return myModel;
    }


    -(IBAction)myBtnPressed:(UIButton *)sender
    {
        [myLabel setText:[NSString stringWithFormat:@"%g / 15", [myModel generateRandomNumber]]];
    }

    @end

Also, in the end, I want to make generateRandomNumber return a random number between 0 and 15. How would I do this? Would a simple line like:

int x = arc4random() % 16;

work for this? Or do I have to seed it in some way so it doesn't always return the same values when I run the application?

Thanks in advance.


It doesn't return the 1.34 and I don't understand why it doesn't. Can someone clear this up?

Almost certainly, you haven't allocated and initialised the myModel object. You can send messages to nil without crashing but the return value will be 0.

arc4random() doesn't need a seed.

Edit

Your init code looks OK but you are not calling int, in your myBtnPressed: method, you need

[myLabel setText:[NSString stringWithFormat:@"%g / 15", [[self myModel] generateRandomNumber]]];


Are you instantiating an object of your model type? I'm asking because you say that you have declared the function in myModel.h (Could be a typo).

And yes - to get a random number between 0 and X:

int rand = arc4random() % X;

And you don't need to seed the generator.

To return a double between 0 and 15:

// define this somewhere
#define ARC4RANDOM_MAX      0x100000000

// and then use this
double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 15.0f);

random-thoughts-rand-vs-arc4random.html for more.


Did you ever alloc/create the myModel object?

I'm guessing that you didn't, and you're just trying to use the class as a 'methods' class that doesn't store anything (I know there's a name for it, but I'm self taught so my terminology is pretty horrible!)

You can do this in objective-c, but you've got to use different syntax. Instead of using minus signs for the method declaration, use "+":

+(double)generateRandomNumber;

and now your method should be usable!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜