swift - 在将其保存到属性时追加字符串并在调用它时将其删除

标签 swift xcode

我喜欢在eventTextField.text中添加"abc",保存到下面的属性eventName

eventInformation.setValue(eventTextField.text, forKey: "eventName")

我试过了

let abcString = "abc"
eventInformation.setValue(eventTextField.text?.append(abcString), forKey: "eventName")

但我的应用因以下错误而崩溃

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "eventName"; desired type = NSString; given type = _SwiftValue; value = ().'

最佳答案

错误发生是因为append正在改变但没有返回任何东西,
那是 _SwiftValue;值 = () (无效)

您需要一个额外的步骤:

let abcString = "abc"
eventTextField.text?.append(abcString)
eventInformation.setValue(eventTextField.text, forKey: "eventName")

关于swift - 在将其保存到属性时追加字符串并在调用它时将其删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505744/

相关文章:

ios - 如何更改 UINavigationController 推送过渡样式?

objective-c - 忽略单个文件中的 "Unused Entity Issue: Unused Variable"

javascript - ios - 如何在 Web View 中使用 javascript 打开快速 View ?

ios - 如何在下载图像之前运行事件指示器?

ios - 代码 : The file “XXX.entitlements” couldn’t be opened because there is no such file

xcode - 在 XCode/swift 中检测应用程序崩溃

iphone - -[__NSArrayM insertObject :atIndex:]: object cannot be nil

swift - 为什么常量约束来自结构实例而不是类实例的属性?

oop - 为什么协议(protocol)的关联类型在 Swift 中不使用泛型语法?

Swift firebase 查询有序搜索