我正在开发Swift中的应用程序,并且我有一个文件ToursObject.swift:
import Foundation
class ToursObject {
var nameFr: String!;
var nameEn: String!;
var nameEs: String!;
var nameIt: String!;
init(json: NSDictionary) {
self.nameFr = json["name_fr"] as? String;
self.nameEn = json["name_en"] as? String;
self.nameEs = json["name_es"] as? String;
self.nameIt = json["name_it"] as? String;
}
}
在我的tableViewController中,我有以下代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// custom cell
let cell = tableView.dequeueReusableCellWithIdentifier("toursCellReuseIdentifier") as! ToursTableViewCell;
// here i get the actual preferredLanguage from previous screen which propose en, fr, it and es
let codeCountry = NSLocale.preferredLanguages()[0] as String;
// if we print we will have En, Fr, It or Es for codeCountry
// I want do something like that but of course when i build I have this error message from xCode : Value of type 'ToursObject" has no member 'name'
cell.titleLabel.text = (self.arrayOfAvailableToursObject[indexPath.item].name+codeCountry);
// work correctly because no concatenation on the name of member
cell.descriptionLabel.text = (self.arrayOfAvailableToursObject[indexPath.item].nameEn);
return cell;
}
问题是这一行,因为我不知道如何进行串联以调用字典的好键:
cell.titleLabel.text = (self.arrayOfAvailableToursObject[indexPath.item].name+codeCountry);
我认为不可能像我想的那样做,但是还有另一种解决方案?
预先感谢您的帮助。
那么问题是您需要按语言动态访问事物吗?在这种情况下,您可以通过将JSON反序列化为具有具体属性的对象来为自己做更多的工作。为什么不让它保持动态?
class ToursObject {
var nameForCountry: [String:String]!;
init(json: NSDictionary) {
self.nameForCountry = // deserialize into your typed dictionary;
}
}
然后,您可以按以下方式访问:
cell.titleLabel.text =
self.arrayOfAvailableToursObject.nameForCountry[codeCountry]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句