[英]Why crash when I set CGFloat value to AssociatedObject by assign policy In Swift?
static var testValue = 1
objc_setAssociatedObject(self, &testValue, 10.1, .OBJC_ASSOCIATION_ASSIGN)
objc_getAssociatedObject(self, &testValue)
When I execute the third line of code, it will crash , But when I change the value that I want to set to 10(or 10.0) , it can successfully get the value.当我执行第三行代码时,它会崩溃,但是当我将要设置的值更改为10(或 10.0) 时,它可以成功获取该值。 Why this happen?为什么会发生这种情况?
Associated values store pointer sized items (typically eight bytes).关联值存储指针大小的项目(通常为 8 个字节)。 When you try to read it, it will read a pointer sized item and overwrite eight bytes of memory.当您尝试读取它时,它将读取一个指针大小的项目并覆盖八个字节的内存。 If the variable you are using is four bytes only, then four bytes of random memory are overwritten, with all kinds of consequences.如果您使用的变量只有四个字节,那么四个字节的随机内存将被覆盖,从而产生各种后果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.