Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CocoaBean

macrumors newbie
Original poster
Feb 23, 2009
25
0
Hi,

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:

Appcontroller.h

Code:
#import <Cocoa/Cocoa.h>


@interface AppController : NSObject {
	
	IBOutlet NSButton *btnGetNumbers;
	IBOutlet NSButton *btnClear;
	IBOutlet NSTextField *txtNumbers;

}

-(IBAction)getNumbers:(id)sender;
-(IBAction)clear:(id)sender;

@end

Appcontroller.m

Code:
#import "AppController.h"


@implementation AppController

-(IBAction)getNumbers:(id)sender
{
	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];
}


-(IBAction)clear:(id)sender
{
	[txtNumbers setStringValue:@""];
}


@end

Also, is this code the correct way to go about this task?

Thanks
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
From the random manpage:
Code:
     long
     random(void);

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.

-Lee
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Code:
-(IBAction)getNumbers:(id)sender
{
	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];

Can you explain why on earth you feel the necessity to create six objects of type NSNumber?
 

CocoaBean

macrumors newbie
Original poster
Feb 23, 2009
25
0
Can you explain why on earth you feel the necessity to create six objects of type NSNumber?

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).
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
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).

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.