Ios 如果数组中不存在对象,如何显示备用消息?目标C

Ios 如果数组中不存在对象,如何显示备用消息?目标C,ios,objective-c,Ios,Objective C,本应用程序的目标是在英语和西班牙语单词之间进行翻译。 (对照所有数组值检查文本输入,查看它是否存在,然后将该索引与第二个数组进行比较,并显示并行值) 那部分正在发挥作用。如果输入的单词在数组中不存在,我应该在标签中显示类似“无翻译可用”的消息。我的问题是,我可以让消息不显示任何内容,也可以显示所有内容,而不仅仅是在它应该显示的时候 #import "TranslatorViewController.h" @interface TranslatorViewController () @prop

本应用程序的目标是在英语和西班牙语单词之间进行翻译。 (对照所有数组值检查文本输入,查看它是否存在,然后将该索引与第二个数组进行比较,并显示并行值)

那部分正在发挥作用。如果输入的单词在数组中不存在,我应该在标签中显示类似“无翻译可用”的消息。我的问题是,我可以让消息不显示任何内容,也可以显示所有内容,而不仅仅是在它应该显示的时候

#import "TranslatorViewController.h"

@interface TranslatorViewController ()

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)translate:(id)sender;

@property (nonatomic, copy) NSArray *english;
@property (nonatomic, copy) NSArray *spanish;

@end


@implementation TranslatorViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.delegate = self;
}

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

//make the keyboard go away
-(BOOL) textFieldShouldReturn:(UITextField *)textField {

{[textField resignFirstResponder];}
return YES;
}


- (instancetype)initWithCoder:(NSCoder*)aDecoder {     self = [super initWithCoder:aDecoder]; if(self) {     // Add your custom init code here

self.english = @[@"phone",
                 @"dog",
                 @"sad",
                 @"happy",
                 @"crocodile"];
self.spanish = @[@"telefono",
                 @"perro",
                 @"triste",
                 @"felize",
                 @"cocodrilo"];
}  return self; }



- (IBAction)translate:(id)sender {


//loop through the array
NSString *englishWord = self.textField.text;

for (int index=0; index<[self.english count]; index++)
    if([[self.english objectAtIndex:index]
    isEqualToString:englishWord])

    //retrieve the accompanying spanish word if english word exists in the array

{NSString *spanishWord = [self.spanish objectAtIndex:index];
    //and display it in the label
    self.label.text = spanishWord;}

   //Need code to display 'no translation' if word was not found.

}



@end
#导入“TranslatorViewController.h”
@接口TranslatorViewController()
@属性(弱,非原子)IBUITEXTFIELD*textField;
@属性(弱,非原子)IBUILabel*标签;
-(iAction)翻译:(id)发送者;
@财产(非原子,副本)NSArray*英文;
@财产(非原子,副本)NSArray*西班牙文;
@结束
@TranslatorViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
_textField.delegate=self;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
//把键盘拿走
-(BOOL)textField应返回:(UITextField*)textField{
{[textField resignFirstResponder];}
返回YES;
}
-(instancetype)initWithCoder:(NSCoder*)aDecoder{self=[super initWithCoder:aDecoder]; if(self){//在此处添加自定义初始化代码
self.english=@[@“电话”,
@“狗”,
@“悲伤”,
@“快乐”,
@“鳄鱼”];
self.西班牙语=@[@“telefono”,
@“佩罗”,
@“三重态”,
@“费利兹”,
@“cocodrilo”];
}回归自我;}
-(iAction)翻译:(id)发件人{
//在数组中循环
NSString*englishWord=self.textField.text;

对于(int index=0;index来说,最简单的方法可能是在进入循环之前将标签的文本字段设置为“No translation”。如果没有找到匹配项,标签将永远不会被重新设置为任何其他内容

有很多其他的方法来构造逻辑以获得这个结果。我可以通过这样做来收紧最后一个代码循环:

NSString * englishWord = self.textField.text;

NSUInteger spanishWordIndex = [self.english indexOfObject:englishWord];
if (spanishWordIndex == NSNotFound) {
    self.label.text = @"No translation";
} else {
    self.label.text = self.spanish[spanishWordIndex];
}

为什么不用词典呢

- (instancetype)initWithCoder:(NSCoder*)aDecoder {     
     self = [super initWithCoder:aDecoder]; 
     if(self) {     // Add your custom init code here

        self.translationDict = @{@"phone":@"telefono",
                 @"dog":@"perro",
                 @"sad": @"triste",
                 @"happy": @"felize",
                 @"crocodile": @"cocodrilo"]; // self.translationDict is an NSDictionary

     }  
     return self; 

 }


 - (IBAction)translate:(id)sender {
      NSString *englishWord = self.textField.text;
      NSString *spanishWord=self.translationDict[englishWord];
      if (spanishWord == nil) {
         spanishWord="No Translation";
      }
      self.label.text=spanishWord;
 }

重新格式化答案以包含您的整个代码。您应该能够将粘贴复制到您的代码中

- (IBAction)translate:(id)sender {

    NSString *englishWord = self.textField.text;
    NSString *spanishWord = @"No translation found.";

    for (int index=0; index<[self.english count]; index++)
    {
        if([[self.english objectAtIndex:index] isEqualToString:englishWord])
        {
            //retrieve the accompanying spanish word if english word exists in the array
            spanishWord = [self.spanish objectAtIndex:index];
        }
    }

    self.label.text = spanishWord;

}
-(iAction)翻译:(id)发送方{
NSString*englishWord=self.textField.text;
NSString*spanishWord=@“未找到翻译。”;
对于(int-index=0;indexI)

self.label.text=@“无可用翻译”

在if声明之前,我相信Ben Zotto想说的话!我只是不知道一开始该怎么做。 我是个新手

但这正是我所需要的


谢谢大家!

但是,例如,如果我输入了一个存在的单词,然后尝试输入一个不存在的单词,它会恢复为默认值吗?@Kate:我不确定你的确切意思,但你可以这样想——在事件循环的最后,你设置标签的最后一件事就是屏幕上显示的内容(直到您下次更改它)。因此每次调用
translate
操作时,它都会逐步执行此逻辑,并将其设置为结果或“无翻译”占位符。该代码使“没有翻译显示一切”。除此之外,您可能会考虑使用<代码> NSCORCHORE < /Code >来映射英语和西班牙语之间的单词。代码将更干净+它将更具性能(对4个项目来说不重要,但为此使用词典是一个好习惯)。。这是行不通的,因为即使您有翻译,它也会在后续循环中显示“无翻译可用”消息。例如:代码进入循环,它会将标签设置为“无翻译可用”,检查是否有。如果找到标签,它会正确设置翻译。然后它会查看t中的下一个元素在数组中,将标签设置为“无翻译可用”,删除翻译。将其设置在for循环之前应该有效。