Objective-C前向方法声明

时间:2013-04-27 20:28:03

标签: objective-c

我为一个类编写了一个方法formatSearchString,我试图在它实现之前调用它(应该不重要?)。我收到这个错误:

Error: Semantic Issue
Use of undeclared identifier 'formatSearchString'

我正在使用XCode 4.6.2

接口文件FHViewController.h

#import <Foundation/Foundation.h>

@interface FHViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate>
@property(strong, nonatomic) NSString *searchTerm;
- (NSString *)formatSearchString:(NSString *)userEntry;
@end

实施文件FHViewController.m

#import "FHViewController.h"

@interface FHViewController()
- (NSString *)formatSearchString:(NSString *)userEntry;
@end

@implementation FHViewController
@synthesize searchTerm;

-(void)viewDidLoad
{
     [super viewDidLoad];
     NSString *formatted = [formatSearchString userEntry:searchTerm];
}

- (NSString *)formatSearchString:(NSString *)userEntry
{
     NSLog(@"User Entry: %@", userEntry);
     return @"Dummy string for now";
}
@end

3 个答案:

答案 0 :(得分:4)

NSString *formatted = [formatSearchString userEntry:searchTerm];

那条线是错的。请注意区别:

NSString *formatted = [self formatSearchString:searchTerm];

答案 1 :(得分:0)

您使用formatSearchString作为变量名,而不是方法名,您需要在对象上调用formatSearchString:

NSString *formattedString = [self formatSearchString:mySearchString];

答案 2 :(得分:0)

Objective C中方法调用的语法是[receiver method: param1 ...]。因此,您需要将代码更改为:

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSString *formatted = [self formatSearchString: searchTerm];
}