I am trying to write my first class in objective-c and i want to pass an NSString object as a parameter in a method of the class, however i get an error saying that "can not use an object as parameter to a method". So my question is how do you pass a string as a parameter to a method? here is my code tht i am trying to get to work:
#import <Foundation/Foundation.h>
@interface Car
{
NSString *color;
NSString *make;
int year;
}
- (void) print;
- (void) setColor: (NSString) c;
- (void) setMake: (NSString) m;
- (void) setYear: (int) y;
@end
@implementation Car
- (void) print
{
NSLog(@"The $d Ford %@ is $@.", year, make, color);
}
- (void) setColor: (NSString) c
{
color=c;
}
- (void) setMake: (NSString) m
{
make=m;
}
- (void) setYear: (int) y
{
year=y;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Car *car= [Car new];
[car setColor:@"blue"];
[car setMake:@"Bronco"];
[car setYear:1992]
[car print];
[car release];
[pool drain];
return 0;
}
You need to hand in a pointer to your string object. So you need to add a * to the type of the parameter:
f.e.
- (void) print;
- (void) setColor:(NSString *) c;
- (void) setMake:(NSString *) m;
- (void) setYear:(int) y;
and the same in the implementation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With