2

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:

  1. How do I get rid of the warning above?
  2. Instead of creating methods in the Dog Class for setting Age and Name, how could I have make Age and Name public class level variables that I could directly assign to?

Any help would be much appreciated!

Thanks, Pete

DigiOz Multimedia
  • 1,156
  • 1
  • 12
  • 28
  • 1
    I have to point out that decorating your variable names is poor [Cocoa Style](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html#//apple_ref/doc/uid/10000146-SW1). If you see any examples or Public code, such as on GitHub, you won't see many examples of the way you are doing it. Also, you're writing setters, without writing setters, so your class isn't KVC compliant. Better still, just declare and synthesize properties for your variables instead. – Abizern Dec 25 '11 at 13:24
  • A little tip: Try use prifix your local variables like: _age and _name, a example of an setter would be: - (void)SetAge:(NSInteger)age { _age = age;} or user Properties and synthesize as: @synthesize age = _age. – Justin Dec 25 '11 at 13:25
  • Thanks Abizern, I will review the Cocoa Style guidelines to bring it closer to the Standard. – DigiOz Multimedia Dec 25 '11 at 22:27

2 Answers2

9

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;
Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • 3
    `NSString` properties should use `copy` instead of `retain`. – Mark Adams Dec 25 '11 at 07:29
  • age should really be `NSUInteger` as well. – Abizern Dec 25 '11 at 14:20
  • A pleasure to help! Also, consider Mark Adams' advice above regarding the copy specifier. [Here is a fabulous post](http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain) explaining this. – Jeremy Dec 27 '11 at 15:59
1

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.

thomas.g
  • 3,894
  • 3
  • 29
  • 36
  • Thanks for the info lux. It is rather strange for me to go from C# and Java back to C style way of thinking (and object-c seems to add to the complexity quite a bit too). But I am sure I will get used to it again in time. – DigiOz Multimedia Dec 25 '11 at 22:39
  • I know, I.ve been there too... :) I strongly recommend you to read the Objective-C programming guide (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html) AND the memory management guide (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i)even if you'll use ARC. They helped me a lot when I came back from C#/Java to Objective-C. The rest is more or less learning an big API ;) – thomas.g Dec 26 '11 at 02:37