我有一个块来更新每个String的视图。在它的对象类中,我通过它传递:
func eachFeaturesSection(block: ((String?) -> Void)?) {
propertyFeatures.forEach { feature in
guard let feature = feature as? RealmString else {
return
}
let features = feature.stringValue
block?(features)
}
}
我将在ViewController
中通过:
listing!.eachFeaturesSection({ (features) in
print(features)
self.facilities = features!
})
因此它将打印为:
Optional("String 1")
Optional("String 2")
和self.facilities将设置为最新值self.facilities = "String 2"
cell.features.text = features // it will print String 2
那么,如何在一个字符串中将所有字符串连接在一起,例如self.facilities = "String 1, String 2"
。我用.jointString不起作用。谢谢你的帮助。
答案 0 :(得分:5)
也许您可以将它们添加到String
元素数组中,然后在完成后,在该数组上调用joined
。
在ViewController中是这样的:
var featuresArray = [String]()
listing!.eachFeaturesSectionT({ (features) in
print(features)
featuresArray.append(features!)
})
//Swift 3 syntax
cell.features.text = featuresArray.joined(separator: ", ")
//Swift 2 syntax
cell.features.text = featuresArray.joinWithSeparator(", ")
希望对你有所帮助。
答案 1 :(得分:1)
self.facilities = features!
什么都不做,但每次迭代都会不断更新值
将行self.facilities = features!
更改为self.facilities += features!
或self.facilities = self.facilities + ", " + features!
答案 2 :(得分:0)
以下是我的做法(假设您的propertyFeatures
是一个RealmString
数组):
斯威夫特3:
let string = (propertyFeatures.map { $0.stringValue }).joined(separator: ", ")
斯威夫特2:
let string = (propertyFeatures.map { $0.stringValue }).joinWithSeparator(", ")