PDA

View Full Version : Casting problem from a pointer




CocoaBean
Mar 29, 2009, 12:31 PM
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


#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


#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
Mar 29, 2009, 12:47 PM
From the random manpage:
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: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNumber/initWithLong:) as well as +numberWithLong: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSNumber/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

CocoaBean
Mar 29, 2009, 01:20 PM
Thnaks Lee, I'll give it a go :)

gnasher729
Mar 29, 2009, 05:30 PM
-(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
Mar 30, 2009, 10:52 AM
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
Mar 30, 2009, 12:34 PM
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.

CocoaBean
Mar 30, 2009, 01:04 PM
Thanks, I'll see if I can do that - thanks for the explanation too :)