0

我已经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
4

3 回答 3

4
NSString *formatted = [formatSearchString userEntry:searchTerm];

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

NSString *formatted = [self formatSearchString:searchTerm];
于 2013-04-27T20:32:01.927 回答
0

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

NSString *formattedString = [self formatSearchString:mySearchString];
于 2013-04-27T20:31:54.440 回答
0

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

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSString *formatted = [self formatSearchString: searchTerm];
}
于 2013-04-27T20:32:21.910 回答