Message sent to deallocated instance with ARC using custom getter and setter

旧街凉风 提交于 2019-12-06 07:55:54

The problem is here:

id obj;   
[invocation getArgument:&obj atIndex:2];

getArgument simply copies the object pointer into obj without retaining it. However, since obj is (by default) a __strong variable, it will be released at the end of the current method. To solve the problem, use

__unsafe_unretained id obj;   
[invocation getArgument:&obj atIndex:2];   

Note also that your getter implementation does not work. For example, setFirstName: stores the key in the dictionary using the key "FirstName", but the getter firstName tries to read the value for the key "firstName".

(As already mentioned in a comment, it would probably easier and less error-prone to just override the accessor methods for the three properties separately, instead of dynamic forwarding.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!