Error: Trying to put the stack in unreadable memory at:

Viewed 7810

I am trying to add additional properties to UIViewController.

Code:

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)}
    }
}

But I am getting the following error:

Error: Trying to put the stack in unreadable memory at:

I know that we cannot directly add stored properties to extensions so I am trying it add using objc_setAssociatedObject()

2 Answers

If someone has the below scenario

If your method is getting called recursively, you may get this error.

Related