我想向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()
謝謝你的寶貴意見,並指出我到底出錯的地方。我應該注意到遞歸調用self的錯誤。 – user2879160
@ user2879160樂意幫忙! :) – Hamish