Reputation: 1176
After programming with C# and Java for many years I finally decided to learn Objective-C in order to start programming iOS Devices as well as Mac OS X, and I have to admit it is very different then most modern c-based programming languages. I am getting the following warning in my code:
warning: passing argument 1 of 'SetAge' makes pointer from integer without a cast
Here is my code:
Dog.h
#import <Cocoa/Cocoa.h>
@interface Dog : NSObject {
int ciAge;
NSString * csName;
}
- (void) Bark;
- (void) SetAge: (int *) liAge;
- (void) SetName: (NSString *) lsName;
@end
Dog.m
#import "Dog.h"
@implementation Dog
- (void) Bark
{
NSLog(@"The dog %@ barks with age %d", csName, ciAge);
}
- (void) SetAge: (int *) liAge {
ciAge = (int)liAge;
}
- (void) SetName: (NSString *) lsName {
csName = lsName;
}
@end
HelloWorld.m
#import <Foundation/Foundation.h>
#import "Dog.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int liTemp = 75;
NSString * lsCity = @"Chicago";
NSDate * loDate = [NSDate date];
// insert code here...
NSLog(@"The temperature is %d currently in %@, on %@", liTemp, lsCity, loDate);
int liAge = 10;
// Call Dog class
Dog * Dog1 = [Dog new];
[Dog1 SetAge:(int)liAge]; // The Warning happens here
[Dog1 SetName:@"Fido"];
[Dog1 Bark];
[pool drain];
return 0;
}
My Questions Are:
Any help would be much appreciated!
Thanks, Pete
Upvotes: 2
Views: 7393
Reputation: 3932
int
is a C primitive scalar type so you don't need pointers. Don't use *
.
Warning here
int *
represents a pointer variable, a thing you're not used to see in C# or in Java.
int *p
is a variable that will point to a memory address. To put data at this address you have to dereference the variable p before using it ex:*p = 3
.
Objective-C is based on the C language and this is a C language problem. You should (must ?) read about C and pointers if you want to code in Objective-C.
And read also how Objective-C simplifies your life with pointers to objects particularly the fact that you don't have do explicitly dereference them to use them.
Upvotes: 1
Reputation: 9030
Don't declare an int as a pointer. Change your code from:
- (void) SetAge: (int *) liAge
to
- (void) SetAge: (int) liAge
and
- (void) SetAge: (int *) liAge {
ciAge = (int)liAge;
}
to
- (void) SetAge: (int) liAge {
ciAge = liAge;
}
Consider making age and name a property. Change:
- (void) SetAge: (int *) liAge;
- (void) SetName: (NSString *) lsName;
to
@property (nonatomic, readwrite) NSInteger age; //Don't declare as pointer
@property (nonatomic, retain) NSString *name; //DO declare as pointer
Also, don't forget to synthesize them in your implementation file:
@synthesize age, name;
Upvotes: 9