Casting problem from a pointer

Discussion in 'Mac Programming' started by CocoaBean, Mar 29, 2009.

  1. macrumors newbie

    Feb 23, 2009

    I seem to struggle quite a lot when it comes to casting and I was creating a lottery application which generates 6 random numbers then displays the results in a textfield (label).

    I get the following warning, 'assignment makes pointer from integer without a cast'.

    I'm a bit stumped as what to do and how and where to make the appropriate cast.

    The code is below:


    #import <Cocoa/Cocoa.h>
    @interface AppController : NSObject {
    	IBOutlet NSButton *btnGetNumbers;
    	IBOutlet NSButton *btnClear;
    	IBOutlet NSTextField *txtNumbers;

    #import "AppController.h"
    @implementation AppController
    	NSNumber *number1 = [[NSNumber alloc] init];
    	NSNumber *number2 = [[NSNumber alloc] init];
    	NSNumber *number3 = [[NSNumber alloc] init];
    	NSNumber *number4 = [[NSNumber alloc] init];
    	NSNumber *number5 = [[NSNumber alloc] init];
    	NSNumber *number6 = [[NSNumber alloc] init];
    	number1 = random() % 49 + 1; //error below each line
    	number2 = random() % 49 + 1; //
    	number3 = random() % 49 + 1; //
    	number4 = random() % 49 + 1; //
    	number5 = random() % 49 + 1; //
    	number6 = random() % 49 + 1; //
    	NSString *string = [NSString stringWithFormat:@"Your numbers are: %i, %i, %i, %i, %i, %i", number1, number2, number3, number4, number5, number6];
    	[txtNumbers setStringValue:string];
    	[number1 release];
    	[number2 release];
    	[number3 release];
    	[number4 release];
    	[number5 release];
    	[number6 release];
    	[super dealloc];
    	[txtNumbers setStringValue:@""];
    Also, is this code the correct way to go about this task?

  2. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    From the random manpage:
    So random returns a primitive, namely, a long int. % acts on primitives, + acts on primitives. a long is not an NSNumber (or NSNumber *, more specifically). NSNumber does have a method -initWithLong: as well as +numberWithLong:. These will initialize an alloc'd NSNumber or return an NSNumber * with the long value, respectively. You should be able to wrap your equation in these and get what you want.

  3. thread starter macrumors newbie

    Feb 23, 2009
  4. macrumors G5


    Nov 25, 2005
    Can you explain why on earth you feel the necessity to create six objects of type NSNumber?
  5. thread starter macrumors newbie

    Feb 23, 2009
    No, I can't explain as I'm still learning and I'm not sure myself.

    Can you please tell me why that code is wrong and say what I should have done instead?

    Thanks :)

    edit: The program is supposed to display 6 random numbers in the range 1 to 49. Is there a simpler way of writing the code? Also, I'm not sure about the equation: random() % 49 + 1; (there was similar code in a cocoa book to get numbers in the range 1 to 100, so I modified it to get 1 to 49, although not quite understanding it).
  6. macrumors regular

    Oct 13, 2008
    Achewood, CA
    Use an array of six long ints and a for loop to generate the numbers.

    Or, if you wanted to be more Cocoa-like, you could use an NSArray with six NSNumber objects, but this involves more overhead.

    random() returns a random number between 0 and 2,147,483,647. % is the modulus (remainder after division) operator, it reduces the output of random() to a value between 0 and 48. Then it's just a simple matter of adding 1, and you have a random number between 1 and 49.
  7. thread starter macrumors newbie

    Feb 23, 2009
    Thanks, I'll see if I can do that - thanks for the explanation too :)

Share This Page