Why is the Obj-C property property by default "assign" instead of "strong"

I am adding Swift classes to an old project. Everything went well until I tried to add a property to the Swift class. The generated header is not compiled.

enter image description here

I think the problem is that in the generated code, Swift missed the strong

property and only declared it as nonatomic

. Usually that should be enough, because @property should be owned by default strong

, right?

So, they are basically equivalent:

  • @property (nonatomic) NSDate *aDate;

  • @property (nonatomic, strong) NSDate *aDate;

But, in my case, it looks like the default message assign

instead strong

of the default matches the compiler message.

I am using Xcode 6 GM and the project has ARC enabled.

Any idea why it is failing strong

? Can I change this?

+3


source to share


2 answers


After a lot of experimentation, I discovered a subtlety defining the default property owner behavior:

  • If a header file is only imported into ARC-enabled classes , and is not declared by default, then ownership of the property in that header file strong

    .
  • If the header file is imported into at least one non-ARC class and is not declared by default, then the ownership of that property assign

    !

This means that you should not import the header -Swift.h

in any non-ARC classes, as this will change the behavior of all properties and generate warnings (which in my case were converted to errors).

Quite strange IMHO ...




Example:

  • We have classes SourceClass

    , ARCClass

    (ARC-enabled) and MRCClass

    (ARC-disabled)
  • SourceClass.h

    It has: @property (nonatomic) NSDate *date;

Stealth:

  • If we add #import "SourceClass.h"

    only to ARCClass.h

    or ARCClass.m

    ,
    • the property date

      has ownership strong

      .
    • declaration is equivalent @property (nonatomic, strong) NSDate *date;

      .
  • As soon as we add #import "SourceClass.h"

    to MRCClass.h

    or MRCClass.m

    ,
    • property date

      will contain assign

      instead of
      .
    • announcement changed to @property (nonatomic, assign) NSDate *date;

      .
+2


source


I'm sure that once "assign" was the default and that ...

http://cagt.bu.edu/w/images/b/b6/Objective-C_Programming_Language.pdf

assign - Indicates that the installer uses a simple assignment. This is the default.

... seems to confirm this (p. 59).



However, I also see an Apple document (Programming with Objective-C) that says, "By default, both Objective-C properties and variables support strong references to their objects." I believe the change was made with the introduction of ARC.

Although you say ARC is enabled, if this project is old enough, there might be something still around to interfere with ARC settings.

I realize this is not a definitive answer, but perhaps checking the project settings (or cleaning the project) with this change in mind might help.

+2


source







All Articles