简体   繁体   English

当我在 Swift 中通过分配策略将 CGFloat 值设置为 AssociatedObject 时,为什么会崩溃?

[英]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.

相关问题 关于Swift 4中的返回CGFloat值 - About return CGFloat value in Swift 4 为什么在Swift中设置时Enum的值没有变化? - Why the value of Enum didn't change when I set in Swift? 为什么我在不同的设备中获得不同的CGFloat价值 - why I am getting different CGFloat value in different devices 无法将 CGFloat 类型的值分配给 NSLayoutConstraint - Cannot assign value of type CGFloat to NSLayoutConstraint 快速分配值时如何避免设置方法? - How to avoid set method when assign the value in swift? 什么是“(UIView)-> CGFloat”? 我正在尝试获取 CGFloat 值 - What is a "(UIView) -> CGFloat"? I am trying to get a CGFloat value swift无法将类型(_,_)-> _的值转换为预期的参数类型'((_,CGFloat))-> _ - swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _ 使用 ViewController 中的 CGFloat 值在 Swift 中的 TabBarController 中移动视图 - Using a CGFloat value from ViewController to move a view in TabBarController in Swift 应用程序崩溃我在Swift中使用CGImageSourceCreateThumbnailAtIndex - App Crash When I use CGImageSourceCreateThumbnailAtIndex in Swift 无法赋值:'i'是swift中的'let'常量 - Cannot assign to value: 'i' is a 'let' constant in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM