如果阵列中不存在对象,如何显示备用消息?目标C.

时间:2014-09-18 22:21:36

标签: ios objective-c

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

那部分正在发挥作用。如果输入的单词在数组中不存在,我应该有一条消息,如“没有可用的翻译”和“#39;显示在标签上。我的问题是,我可以将消息显示为什么都不显示 - 而不是仅仅应该显示它。

#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

4 个答案:

答案 0 :(得分:1)

最简单的方法是将标签的文本字段设置为&#34;无翻译&#34; 进入循环之前。如果未找到匹配项,则标签将永远不会重新设置为其他任何内容。

还有很多其他方法可以构建逻辑来为您提供此结果。我可以通过这样做来收紧最后一行代码:

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];
}

答案 1 :(得分:0)

为什么不使用NSDictionary?

- (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;
 }

答案 2 :(得分:0)

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

- (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;

}

答案 3 :(得分:0)

我把

self.label.text = @&#34;无法翻译&#34 ;;

在if语句之前,我相信Ben Zotto试图说的话!我一开始并不确定如何真正做到这一点。 我是这个东西的新手。

但这就是我所需要的。

全部谢谢!