我正在尝试向 UIViewController 添加其他属性。
代码:
protocol AdditionalStoredProperties
{
associatedtype Title
func getAssociatedObject<Title>(key: UnsafePointer<Title> ,
defValue : Title)->Title
}
extension AdditionalStoredProperties
{
func getAssociatedObject<Title>( key: UnsafePointer<Title> , defValue : Title)->Title
{
guard let actual_value = objc_getAssociatedObject(self as! AnyObject, key) as? Title else
{
return defValue
}
return actual_value
}
}
extension UIViewController:AdditionalStoredProperties
{
typealias Title = String
var previousPage : String
{
get { return getAssociatedObject(&self.previousPage, defValue: self.previousPage) }
set { objc_setAssociatedObject(self, &self.previousPage, newValue, .OBJC_ASSOCIATION_RETAIN)}
}
}
但是我收到以下错误:
错误:试图将堆栈放入不可读的内存中:
我知道我们不能直接将存储的属性添加到扩展中,所以我尝试使用 objc_setAssociatedObject() 添加
最佳答案
如果有人遇到以下情况
If your method is getting called recursively, you may get this error.
关于swift - 错误 : Trying to put the stack in unreadable memory at:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45541575/