selectedLanguageAbbreviation

selectedLanguageAbbreviation

我是反应性可可的新手,需要帮助。我正在搜索,但无法弄清楚。假设我有UIPickerView。在该pickerView中,有4个选项,每种语言都有一个选项,例如德语,英语,西班牙语,法语。在选择语言时,我需要发回英文-zh,法文-fr ...的缩写。
我正在使用MVVM体系结构,这是ViewController中的方法。在那种方法中,我绑定了ViewModel和选定的属性。

- (void)bindeViewModel
{
  RAC(self, selectedLanguageAbbreviation) = self.languageViewModel.observeSelectedLanguage;
}


“ observeSelectedLanguage”是我的信号,其实现是:

-(RACSignal *) observeSelectedLanguage
{
    @weakify(self);
    return [RACObserve(self, selectedLanguage) filter:^BOOL(NSString *value){
    @strongify(self);

    if (value == LocalizedString(myValueString(English)))
    {
        self.selectedLanguageAbbreviation = @"en";
        return self.selectedLanguageAbbreviation;
    }
    else if (value == LocalizedString(myValueString(German)))
    {
        self.selectedLanguageAbbreviation = @"ge";
        return self.selectedLanguageAbbreviation;
    }
    else if (value == LocalizedString(myValueString(French)))
    {
        self.selectedLanguageAbbreviation = @"fr";
        return self.selectedLanguageAbbreviation;

    }
    else if (value == LocalizedString(myValueString(Spanish)))
    {
        self.selectedLanguageAbbreviation = @"sp";
        return self.selectedLanguageAbbreviation;
    }
    else
    {
        self.selectedLanguageAbbreviation = @"en";
        return self.selectedLanguageAbbreviation;
    }
 }];
}


“ myValueString”是用于从枚举返回字符串的宏,因此英语,法语等是枚举的一部分。您可以帮我解释一下如何发送所选语言的缩写吗?谢谢

最佳答案

您正在寻找map而不是filter

return [RACObserve(self, selectedLanguage) map:^id(NSString *value){
    ...
}];


其他建议

同样,在信号内部产生副作用(设置self.selectedLanguageAbbreviation)不是很好的做法,应尽可能避免。您可以这样更改逻辑:

selectedLanguageAbbreviation的值显式绑定到selectedLanguage(在视图模型的-init中):

RAC(self, selectedLanguageAbbreviation) = [RACObserve(self, selectedLanguage) map:^id(NSString* language) {
    // do your mapping here
}];


然后,您可以删除-observeSelectedLanguage方法并将视图控制器的selectedLanguageAbbreviation直接绑定到视图模型的selectedLanguageAbbreviation(如果仍然需要):

- (void)bindeViewModel
{
  RAC(self, selectedLanguageAbbreviation) = RACObserve(self.languageViewModel, selectedLanguageAbbreviation);
}

关于ios - 活性 cocoa 能创造新值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30074419/

10-10 18:37