Reputation: 569
I am trying to learn Cocoa and wanted to develop a very simple game that I had played in my youth. It was originally created in MS Basic. Please note I'm very new to Cocoa and objective c
I have create and object called Defaults. It stores three integer variables, planets, fighters and players.
On the front end, I have three drop down boxes with various defaulted values.
I wanted to understand KVC as well as how to set values but I get the following warning...
"Incompatible integer to pointer sending 'int' to parameter of type 'int *'
[NEW CODE]
DEFAULTS.H
#import #import
@interface Defaults : NSObject {
int fighters;
int planets;
int players;
}
@property (assign) int fighters;
@property (assign) int planets;
@property (assign) int players;
@end
Here is an excerpt of the implementation in my controller:
#import "DefaultsController.h"
#import "Defaults.h"
@implementation DefaultsController
- (id)init
{
self = [super init];
if (self) {
// Initialization code here
Defaults *theDefaults = [[Defaults alloc] init];
NSLog(@"Planets: %@",[theDefaults valueForKey:(@"planets")]);
}
return self;
}
-(void)dealloc
{
[super dealloc];
[Defaults release];
}
-(void) awakeFromNib{
}
-(IBAction) addPlanets:(id)sender{
[theDefaults setValue:[planetsButton titleOfSelectedItem] forKey:@"planets"];
[theDefaults setPlanets:[[planetsButton titleOfSelectedItem] intValue]];
theDefaults.planets = [[planetsButton titleOfSelectedItem] intValue];
NSLog(@"Planets: %i", theDefaults.planets);
NSLog(@"%@", [planetsButton titleOfSelectedItem]);
NSLog(@"%i", [[planetsButton titleOfSelectedItem] intValue]);
}
What am I doing wrong?
Upvotes: 0
Views: 615
Reputation: 17916
You've declared your properties to be integer pointers, but then you're attempting to set them to integer values, not integer pointer values. Remove the asterisks in "int *fighters", etc, to change them to integers.
Pointers are one of the most fundamental and challenging concepts of C-based languages (including Objective-C), and if you aren't clear on the distinction I drew above I suggest you look for some careful explanations of how they work.
Update:
Another issue I see here, which may relate to your question below, is that you're doing some funny things with your logging statements. When you put %@
in a formatting string, you're telling NSLog that you want it to replace that with an Objective-C object. But an int is not an object; it's just a basic C type. Then, you're using key-value coding to retrieve your property value, which is further confusing the issue, because key-value coding is also intended to work with Objective-C objects. Replace that line with the following:
NSLog(@"Planets: %i", theDefaults.planets);
and I think you'll get something more in line with what you're expecting. %i
tells NSLog that you have an integer value to print, and theDefaults.planets
(or the equivalent [theDefaults planets]
) will return the value of the planets property, whether it's an object or not.
Upvotes: 5