小编Kia*_*Dev的帖子

错误:尝试将堆栈放在不可读的内存中:

我正在尝试向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)}
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

错误:尝试将堆栈放在不可读的内存中:

我知道我们不能直接将存储的属性添加到扩展,所以我尝试使用objc_setAssociatedObject()添加它

swift

8
推荐指数
2
解决办法
3991
查看次数

标签 统计

swift ×1