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 online now   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
casting problem McBgnr iPhone/iPad Programming 9 Jul 2, 2010 11:39 AM
Conversion problems from 9.2, 10.2 to 10.3 Frisco Mac Basics and Help 5 May 6, 2005 08:02 PM
iTunes Problems from Switching... Piarco Mac Applications and Mac App Store 4 Feb 9, 2005 09:14 AM
Fixing sleep problems from Jaguar? withnail Mac OS X 10.3 (Panther) Discussion 7 Nov 1, 2003 06:16 PM


All times are GMT -5. The time now is 03:03 PM.

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

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