ios - 在 Swift iOS 上返回经过处理的枚举字符串

标签 ios string swift enums

我刚刚为我的项目创建了一个本地化模块,由于我是 Swift 的新手,所以我想知道以下是否可行。

我有一个这样的枚举:

enum Localizations : String
{
    case StringId1 = "string_to_translate_1"
    case StringId2 = "string_to_translate_2"
    case StringId3 = "string_to_translate_3"
    var localized : String {
        return NSLocalizedString(self.rawValue, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

有了这个枚举,我可以用这个命令得到本地化的字符串:

let myString = Localizations.StringId1.localized

但是当您必须放置大量字符串时,.localized 就显得多余了,因为您之前已经有了 Localizations。 所以我正在寻找的是我是否可以做这样的事情:

let myString = Localizations.StringId1

myString 类似于“按下按钮继续”

我设法做了一些工作,但不是在所有情况下。

在此链接中找到:https://appventure.me/2015/10/17/advanced-practical-enum-examples/ 在“高级枚举使用协议(protocol)”步骤中,它建议像下面这样的修改将得到我想要的:

protocol CustomStringConvertible {
  var description: String { get }
}

enum Trade: CustomStringConvertible {
   case Buy, Sell
   var description: String {
       switch self {
       case Buy: return "We're buying something"
       case Sell: return "We're selling something"
       }
   }
}

let action = Trade.Buy
print("this action is \(action)")
// prints: this action is We're buying something

我的修改是这些:

protocol CustomEnumString {
    var localized: String { get }
}

enum Localizations : String, CustomEnumString
{
    case StringId1 = "string_to_translate_1"
    case StringId2 = "string_to_translate_2"
    case StringId3 = "string_to_translate_3"
    var localized : String {
        return NSLocalizedString(self.rawValue, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

但是当打印它时 if 向我显示了枚举文字,并且当传递给函数时编译器告诉我它是无效的:

let localizedString = Localizations.StringId1
print("localization: \(localizedString)")
// prints: "localization: StringId1"

// note: AlertStrings is an struct with two strings
// this fails to compile saying that cannot convert value of type 'Localizations' to expected argument type 'String'
let alertStrings = AlertStrings(title: Localizations.StringId1, message: Localizations.StringId2)
// this one works, but it's not the purpose I had in mind
let alertStrings = AlertStrings(title: Localizations.StringId1.localized, message: Localizations.StringId2.localized)

所以...简而言之,我希望能够做到这一点:

let localizedString = Localizations.StringId1
print("localization: \(localizedString)")
// prints: "localization: Press Button To Continue"

let alertStrings = AlertStrings(title: Localizations.StringId1, message: Localizations.StringId2)

但在枚举中我只想指定文字一次,而不是先在 case 上然后在 switch 内。

提前致谢!

最佳答案

phelgo , NSBarcelona 的一位成员刚刚告诉我这件事,并且效果很好!

enum Localizations {
    static let StringId1 = NSLocalizedString("string_to_translate_1", comment: "")
}

let myString = Localizations.StringId1

It may look unfamiliar to have an enum with no cases, but we get to keep all of its safety (and code completion), while still preventing Localizations from being instantiated by mistake (if it was a struct)

关于ios - 在 Swift iOS 上返回经过处理的枚举字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199599/

相关文章:

objective-c - 将问题 Objective-C 转换为 Swift

ios - 使用 accessoryView 为 UITableViewCell 的 backgroundColor 设置动画

ios - 找不到框架,链接器命令失败,退出代码为 1(使用 -v 查看调用)

ios - iOS 中的方案或目标

ios - 如何在 Swift - 登录系统中存储变量?

java - 是否可以在 Switch 中使用已有的字符串?

javascript - 在 JavaScript 中将整个字符串转换为整数

Swift:使用变量/参数访问元组元素?

将 Swift CChar 数组转换为字符串

html - 在 UIWebView 中缩放 .htm 文件