Error: trying to put the stack in unreadable memory:

I am trying to add additional properties to a UIViewController.

the 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 get the following error:

Error: trying to put the stack in unreadable memory:

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

+4
source share
1 answer

There are many things that do not match what you are doing:

  • Attempting to access self.previousPageinside its own recipient will call itself recursively.

  • &self.previousPage , ( ). . Swift (. Q & A ).

  • AdditionalStoredProperties ( : class), Objective-C ( Apple Swift ). , , struct AnyObject ( , Obj-C), ; . , , , .

  • , , Title ; ( Title, getAssociatedObject(key:defValue:), ).

, :

protocol AdditionalStoredProperties : class {
    func getAssociatedObject<T>(ofType: T.Type, key: UnsafeRawPointer,
                                defaultValue: @autoclosure () -> T) -> T
}

extension AdditionalStoredProperties {

    func getAssociatedObject<T>(ofType: T.Type, key: UnsafeRawPointer,
                                defaultValue: @autoclosure () -> T) -> T {

        // or: return objc_getAssociatedObject(self, key) as? T ?? defaultValue()
        guard let actualValue = objc_getAssociatedObject(self, key) as? T else {
            return defaultValue()
        }
        return actualValue
    }
}

extension UIViewController : AdditionalStoredProperties {

    private enum AssociatedObjectKeys {
        static var previousPage: Never?
    }

    var previousPage: String {
        get {
            // return the associated object with a default of "" (feel free to change)
            return getAssociatedObject(ofType: String.self,
                                       key: &AssociatedObjectKeys.previousPage,
                                       defaultValue: "")
        }
        set {
            objc_setAssociatedObject(self, &AssociatedObjectKeys.previousPage,
                                     newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }
}

, :

  • static, . , , Swift .

  • @autoclosure defaultValue:, , , , .

  • key:, UnsafeRawPointer, ; , .

  • ofType:. , , .

  • camelCase snake_case, Swift.

+2

Source: https://habr.com/ru/post/1683145/


All Articles