对于' InstanceName'没有可见的@interface;声明选择器'

时间:2014-09-08 12:34:28

标签: automatic-ref-counting

我的名字是鲍勃。我代码为纽约市公园局。我开始学习Objective C并且有一个"语义问题。"我必须有一个盲点,因为我检查了以下所有代码并且没有看到问题。感谢您提供的蚂蚁帮助

"没有可见的@interface' Greeter'声明选择器' setGreetingText ::"

#import <Foundation/Foundation.h>
#import "Greeter.h"


int main (int argc, const char * argv[])
{
    @autoreleasepool {

        Greeter* myGreeter = [[Greeter alloc] init];

// error on this line
        [myGreeter setGreetingText: @"Hello Objective-C!!" : @"Hello VIP Objective-C!!"];

        [myGreeter issueGreeting];
    }
    return 0;
}

======================================

import&#34; Greeter.h&#34;

import&#34; NewGreeter.h&#34;

@implementation Greeter

NewGreeter *VTS;

- (NSString*) greetingText
{
    return [self greetingText];
}

- (void) setGreetingText:(NSString*) newText01
              andThisToo:(NSString*) newText02
{
    greetingText = newText01;
    [VTS setNewGreetingText: newText02];
}

- (void) issueGreeting
{
    NSLog(@"%@", greetingText);
}



#import "NewGreeter.h"

@implementation NewGreeter

- (NSString*) NewGreetingText
{
    return greeting;
}

- (void) setNewGreetingText:(NSString*) newText
{
    greeting = newText;
}

- (void) issueNewGreeting
{
    NSLog(@"%@", [self NewGreetingText]);
}

@end

#import <Foundation/Foundation.h>
#import "NewGreeter.h"

@interface Greeter : NSObject
{
    NSString *greetingText;
}

 - (void) setGreetingText:(NSString*) newText01
              andThisToo:(NSString*) newText02;

  - (NSString*) greetingText;

  - (void) setGreetingText:(NSString*) newText;

  - (void) issueGreeting;



#import <Foundation/Foundation.h>

@interface NewGreeter : NSObject
{
    NSString *greetingText;
}

- (NSString*) NewGreetingText;

- (void) setNewGreetingText:(NSString*) newText;

- (void) issueNewGreeting;

@end

#import <Foundation/Foundation.h>

@interface NewGreeter : NSObject
{
    NSString *greetingText;
}

- (NSString*) NewGreetingText;

- (void) setNewGreetingText:(NSString*) newText;

- (void) issueNewGreeting;

@end

1 个答案:

答案 0 :(得分:1)

您实施名为-setGreetingText:andThisToo:的方法,但您尝试错误地使用它。您需要将呼叫更改为:

[myGreeter setGreetingText: @"Hello Objective-C!!" andThisToo:@"Hello VIP Objective-C!!"];

注意andThisToo位。


如果您只是尝试设置对象的属性,那么您不需要为此公开自定义方法。相反,你可以这样做:

// In Greeter.h
@interface Greeter: NSObject
    @property NSString *greetingText;
@end

// In Greeter.m
@implementation Greeter
    @synthesize greetingText = _greetingText; // Create a backing instance variable
@end

这将创建一个名为setGreetingText的实例变量setter方法和一个名为greetingText的实例变量getter。我建议你阅读creating object properties