Objective-C语法错误 - 意外“@”/无法解决问题

时间:2016-05-19 19:51:47

标签: objective-c uitableview

enter code here [相同代码的youtube图片] [1]我的代码中出现错误

  

程序中的“意外”@“并且还缺少”@end“

如果您查看下面的代码@end是否存在,但无论我重新输入多少,答案都是相同的“缺少@end”

这是完整的代码

#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MasterViewController ()

@end

@implementation MasterViewController
-(NSMutableArray *) objects

{
if (!_objects){
_objects =[[NSMutableArray alloc]init];
}
return _objects;
}

-(NSMutableArray *) results
{
if (!_results) {
_results =[[NSMutableArray alloc]init];
}
return _results;
}
-(void)viewDidLoad
{
[super viewDidLoad];

[self.objects addObject:@"Tabebuia yellow"];
[self.objects addObject:@"Prunus armeniaca"];
[self.objects addObject:@"Tabebuia rosea"];

}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) searchThroughData
{
self.results= nil;
NSPredicate * resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search]%@", self.searchBar.text];
self. results = [[ self.objects filteredArrayUsingPredicate:resultsPredicate]mutableCopy];
}
-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView)
{
return self.objects.count;

}else{

[self searchThroughData];
return self.results.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString  *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if (tableView == self.tableView){
cell.textLabel.text = self.objects[indexPath.row];

} else {
cell.textLabel.text = self.results[indexPath.row];

}
return cell;
}

    -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.searchController.isActive)
{

[self performSegueWithIdentifier:@"Showdetail" sender:self];

}

-(void) PrepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender


if ([[ segue identifier] isEqualToString:@"ShowDetail"]) {

NSString *object = nil;
NSIndexPath *Indexpath = nil;

if (self.UISearchController.isActive)
{UISearchController
    Indexpath = [[ self.searchController searchResultsTableView]indexPathForSelectedRow];
    object = self.results [Indexpath.row];
}else{

       Indexpath = [self.tableView indexPathForSelectedRow];
object = [[self.objects [Indexpath.row];



    [segue destinationViewController] setDetailLabelContents:object];
}}

@end

1 个答案:

答案 0 :(得分:3)

  1. }方法结尾处没有结束prepareForSegue:sender:。正确格式化和缩进使这一点变得清晰。
  2. 您的代码只是浮动在方法之外(tableView:didSelectRowAtIndexPath:方法之前的行。
  3. 需要{}的正确配对,代码需要在方法或函数中。