我有一个 block 可以更新每个字符串的 View 。在它的对象类中,我通过它:
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 不起作用。感谢您的帮助。
最佳答案
也许您可以将它们添加到 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(", ")
希望对你有帮助。
关于swift - 在 block 中连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40906738/