如何在objective-c中传递字符串作为方法参数?

我试图在 objective-c中编写我的第一个类,我想在类的方法中传递NSString对象作为参数,但是我得到一个错误,说“不能使用对象作为方法的参数”.所以我的问题是如何将字符串作为参数传递给方法?这是我的代码我试图开始工作:
#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;
}

解决方法

您需要提交指向字符串对象的指针.所以你需要在参数类型中添加*:

F.E.

- (void) print;
- (void) setColor:(NSString *) c;
- (void) setMake:(NSString *) m;
- (void) setYear:(int) y;

和实施中的相同

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...