Weak and strong property setter attributes in Objective-C

前端 未结 5 2019
北海茫月
北海茫月 2020-11-29 15:31

What is the difference between weak and strong property setter attributes in Objective-C?

@property(retain, [weak/strong]) __attribute__((NSObject)) CFDictio         


        
5条回答
  •  旧时难觅i
    2020-11-29 16:31

    You either have ARC on or off for a particular file. If its on you cannot use retain release autorelease etc... Instead you use strong weak for properties or __strong __weak for variables (defaults to __strong). Strong is the equivalent to retain, however ARC will manage the release for you.

    The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).

    The 'toll free bridging' part (casting from NS to CF) is a little tricky. You still have to manually manage CFRelease() and CFRetain() for CF objects. When you convert them back to NS objects you have to tell the compiler about the retain count so it knows what you have done.

    Its all here.

提交回复
热议问题