我想为快速应用程序中的集合视图添加本地化字符串数组作为数据源。

我写了这个:

let text = [NSLocalizedString("s0",comment: ""),NSLocalizedString("s1",comment: ""),NSLocalizedString("s2",comment: ""),NSLocalizedString("s3",comment: ""),NSLocalizedString("s4",comment: ""),NSLocalizedString("s5",comment: ""),NSLocalizedString("s6",comment: ""),NSLocalizedString("s7",comment: ""),NSLocalizedString("s8",comment: "")]

有没有更简单的方法将所有字符串放入数组中,因为我有1000个本地化字符串,所以看起来不正确。

最佳答案

您始终可以使用格式(例如s%d.4)设置字符串,并围绕该格式构建数组。

var text:NSMutableArray = NSMutableArray()

for index in 1...10 {
    let formattedString = String(format: "s%.4d", index)

    let localizedString = NSLocalizedString(formattedString, comment: "comment")

    text.addObject(localizedString)
}

然后,您声明您的字符串,例如:s0001,s0002,s0003等。

关于ios - 有没有一种简单的方法可以使用swift将NSLocalizedStrings放入数组中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29185651/

10-09 12:51