Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 29, 2009, 12:31 PM   #1
CocoaBean
macrumors newbie
 
Join Date: Feb 2009
Casting problem from a pointer

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
CocoaBean is offline   0 Reply With Quote
Old Mar 29, 2009, 12:47 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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

Last edited by lee1210; Mar 29, 2009 at 12:54 PM.
lee1210 is offline   0 Reply With Quote
Old Mar 29, 2009, 01:20 PM   #3
CocoaBean
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Thnaks Lee, I'll give it a go
CocoaBean is offline   0 Reply With Quote
Old Mar 29, 2009, 05:30 PM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by CocoaBean View Post
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?
gnasher729 is offline   0 Reply With Quote
Old Mar 30, 2009, 10:52 AM   #5
CocoaBean
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Quote:
Originally Posted by gnasher729 View Post
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).

Last edited by CocoaBean; Mar 30, 2009 at 10:56 AM. Reason: update
CocoaBean is offline   0 Reply With Quote
Old Mar 30, 2009, 12:34 PM   #6
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Quote:
Originally Posted by CocoaBean View Post
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.
autorelease is offline   0 Reply With Quote
Old Mar 30, 2009, 01:04 PM   #7
CocoaBean
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Thanks, I'll see if I can do that - thanks for the explanation too
CocoaBean is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Object type Casting? larswik iPhone/iPad Programming 15 Feb 3, 2014 03:26 PM
Student Expelled for Casting a Spell jkcerda Politics, Religion, Social Issues 41 Jan 14, 2014 04:56 PM
Screen-casting / livestream software? blingboy2win Mac Applications and Mac App Store 0 Nov 16, 2012 07:52 AM
Urgent help needed - casting call harrisondavies Community Discussion 0 Oct 4, 2012 04:29 AM

Forum Jump

All times are GMT -5. The time now is 05:25 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC