使用NSInteger的Objective C语义问题

时间:2014-04-05 18:59:17

标签: objective-c uitableview switch-statement

我的switch语句的第一行出现错误,单词“selected”。谁能告诉我这是什么问题?我一直试图弄明白,但还未能成功。如果有人能指出我正确的方向,我将不胜感激。

SecondTableViewController.m

#import "SecondTableViewController.h"
#import "RootTableViewController.h"
#import "ThirdTableViewController.m"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController
{
NSArray *dogs;
NSArray *cats;
NSArray *frogs;
NSArray *gnats;

NSArray *dogSpecs;
NSArray *catSpecs;
NSArray *frogSpecs;
NSArray *gnatSpecs;

NSMutableArray *arrayItems;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
arrayItems = [[NSMutableArray alloc]init];

dogSpecs = [NSArray arrayWithObjects:@"Yellow", @"Black", @"Mean", @"Fast", @"Ugly", nil];
catSpecs = [NSArray arrayWithObjects:@"Smells", @"Hairy", @"Biter", @"Long", nil];
frogSpecs = [NSArray arrayWithObjects:@"Green", @"Yellow", @"Red", nil];
gnatSpecs = [NSArray arrayWithObjects:@"Crash", @"Annoying", @"Sad", nil];

[self SelectedMasterItem:self.selectedIndex];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)SelectedMasterItem:(NSInteger *)selected
{
[arrayItems removeAllObjects];

switch (selected)
{
    case 0:
        arrayItems = [dogSpecs mutableCopy];
        break;

    case 1:
        arrayItems = [catSpecs mutableCopy];
        break;

    case 2:
        arrayItems = [frogSpecs mutableCopy];
        break;

    case 3:
        arrayItems = [gnatSpecs mutableCopy];
        break;

    default:
        break;
}
[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)indexPath
{
return arrayItems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//makes table indentifier
static NSString *simpleTableIdentifier = @"Animal3Cell";

//creates the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if(!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text =arrayItems[indexPath.row];
return cell;
}

@end

SecondTableViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController

@property(nonatomic, strong) NSString *animalName;

@property (nonatomic) NSInteger *selectedIndex;

-(void)SelectedMasterItem:(NSInteger *)selected;

@end

错误

SecondTableViewController.m:64:5: Statement requires expression of integer type ('NSInteger *' (aka 'int *') invalid)

由于

2 个答案:

答案 0 :(得分:1)

NSInteger不是对象,因此不应将其定义为指针。您现在在switch语句中使用selected。由于您已将NSInterger定义为指针,因此编译器抱怨您不允许在switch语句中使用它。

NSInteger selectedIndex;

答案 1 :(得分:0)

这里唯一的NSInteger *(typedefed to int *)是.m的最后一个方法。这应该改为

-(void)SelectedMasterItem:(NSInteger)selected;