ios 使用未声明的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8335980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:42:13  来源:igfitidea点击:

Use of undeclared identifier

objective-ciosxcode

提问by pdenlinger

Am trying to configure a two component picker, and am getting some errors which I have commented out:

我正在尝试配置一个两组件选择器,并且遇到了一些我已注释掉的错误:

Implementation file:

实现文件:

#import "BIDDoubleComponentPickerViewController.h"

@implementation BIDDoubleComponentPickerViewController

@synthesize doublePicker;
@synthesize fillingTypes;
@synthesize breadTypes;

- (IBAction)buttonPressed:(id)sender
{
    NSInteger fillingRow =  [doublePicker selectedRowInComponent:kFillingComponent]; // Use of undeclared identifier 'kFillingComponent'
    NSInteger breadRow = [doublePicker selectedRowInComponent:kBreadComponent]; // Use of undeclared identifier 'kBreadComponent'

    NSString *bread = [breadTypes objectAtIndex:breadRow];
    NSString *filling = [fillingTypes objectAtIndex:fillingRow];

    NSString *message = [[NSString alloc]initWithFormat:@"Your %@ on %@ bread will be right up", filling, bread];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you for your order" 
                                                    message:message 
                                                   delegate:nil 
                                          cancelButtonTitle:@"Great!" 
                                          otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSArray *fillingArray = [[NSArray alloc] initWithObjects:@"Ham", @"Turkey", @"Peanut Butter", @"Tuna Salad", @"Chicken Salad", @"Roast Beef", @"Vegemite", nil];
    self.fillingTypes = fillingArray;

    NSArray *breadArray  = [[NSArray alloc] initWithObjects:@"White", @"Whole Wheat", @"Rye", @"Sourdough Bread", @"Seven Grain", nil];
    self.breadTypes = breadArray;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.doublePicker = nil;
    self.breadTypes = nil;
    self.fillingTypes = nil;
}




#pragma mark - View lifecycle


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if (component == kBreadComponent) { // Use of undeclared identifier 'kBreadComponent'
        return [self.breadTypes.count]; // Expected identifier
        return [self.fillingTypes objectAtIndex:row]; // Use of undeclared identifier 'row'
    }
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    if (component == kBreadComponent) { // Use of undeclared identifier 'kBreadComponent'
        return [self.breadTypes objectAtIndex:row];
        return [self.fillingTypes objectAtIndex:row];
    }
}




@end

Interface file:

接口文件:

#import <UIKit/UIKit.h>

@interface BIDDoubleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) IBOutlet UIPickerView *doublePicker;
@property (strong, nonatomic) NSArray *fillingTypes;
@property (strong, nonatomic) NSArray *breadTypes;

- (IBAction)buttonPressed:(id)sender;


@end

回答by

kBreadComponentand kFillingComponentare not declared anywhere. If they're declared in a header (.h) file, you need to #importit.

kBreadComponent并且kFillingComponent没有在任何地方声明。如果它们是在头文件 (.h) 中声明的,则需要这样做#import

回答by anticyclope

Your numberOfRowsInComponentand titleForRowmethods are similar, and they are wrong: they have two consequent unconditional return, and this means that the second one will never be executed.

你的numberOfRowsInComponenttitleForRow方法是相似的,它们是错误的:它们有两个结果无条件的return,这意味着第二个永远不会被执行。

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    return self.breadTypes.count;
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    if (component == kBreadComponent) {
    return [self.fillingTypes objectAtIndex:row];
}
    return nil; // Or something, empty string for example. Method requires to return a NSString, so you have to return at least nil;
}

Still you have to find kBreadComponentand kFillingComponentand #importthe file with the definitions of them.

您仍然必须找到kBreadComponentkFillingComponent以及#import带有它们定义的文件。