What is the difference in Swift between 'unowned(safe)' and 'unowned(unsafe)'?

前端 未结 4 1610
醉话见心
醉话见心 2020-12-24 06:51

Apple\'s Swift Programming Language Guide mentions the capture specifiers unowned(safe) and unowned(unsafe), in addition to weak

4条回答
  •  时光取名叫无心
    2020-12-24 07:15

    Here is a quote from Apple Developer Forums:

    unowned vs unowned(safe) vs unowned(unsafe)

    unowned(safe) is a non-owning reference that asserts on access that the object is still alive. It's sort of like a weak optional reference that's implicitly unwrapped with x! every time it's accessed. unowned(unsafe) is like __unsafe_unretained in ARC—it's a non-owning reference, but there's no runtime check that the object is still alive on access, so dangling references will reach into garbage memory. unowned is always a synonym for unowned(safe) currently, but the intent is that it will be optimized to unowned(unsafe) in -Ofast builds when runtime checks are disabled.

提交回复
热议问题