gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:40:23 28 4
gpt4 key购买 nike

我刚刚为我的项目创建了一个本地化模块,由于我是 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com