1
votes

I am confused with the "otherwise" and "as long as" usage in the following phrase;

Otherwise ARC does not enforce the Objective-C type system as long as the implementing methods follow the signature of the static type. It is undefined behavior if ARC is exposed to an invalid pointer.

For ARC's purposes, a valid object is one with «well-behaved» retaining operations. Specifically, the object must be laid out such that the Objective-C message send machinery can successfully send it the following messages:

  • retain, taking no arguments and returning a pointer to the object.
  • release, taking no arguments and returning void.
  • autorelease, taking no arguments and returning a pointer to the object.

http://clang.llvm.org/docs/AutomaticReferenceCounting.html

Could we deduce the following;

...(forget the otherwise case)...ARC will enforce Objective-C type system if the implementing methods follow the signature of the static type.

Is it talking about the signatures of retain,release,autorelease methods? If so, does it have the negative meaning;

ARC will _not_ enforce Objective-C type system if the implementing methods follow the signature of the static type.

2

2 Answers

1
votes

First paragraph

Here's a “retainable object pointer” variable with “block pointer type”:

void (^blockPointerTypeVariable)(int);

If you set blockPointerTypeVariable to point at something that is not a block, the behavior of your program is undefined. (You could set it to point at a block with a different type signature without violating ARC's requirements. But you can't set it to point at a UIView.)

Here's a “retainable object pointer” variable with “Class type”:

Class someClass;

Here's a “retainable object pointer” variable with “Class type“ that is “protocol-qualified”:

Class<SomeProtocol> someClassWithProtocol;

If you set someClass or someClassWithProtocol to point at something that is not a Class, the behavior of your program is undefined. (So you can set it to [self class] or [UIView class], but you can't set it to point at an instance of UIView.)

Here's a “retainable object pointer” variable that's not one of the special cases:

UIView *view;

As far as ARC is concerned, you can set this pointer to point to anything, as long as the anything has the same type signatures as UIView does for retain, release, and autorelease. If you point view at something with a different type signature for retain than the signature of -[UIView retain], the behavior of your program is undefined.

Second paragraph

You can set that view variable to point at anything, as long as the anything works with the Objective-C message-sending system (meaning the objc_msgSend function and its variants), and as long as the anything has the described semantics when it receives the retain, release, and autorelease messages.

In particular, this means you can point it at things that aren't instances of Objective-C classes, if you're careful to make the memory layout of those things match what objc_msgSend expects, and as long as you make those things respond properly to retain, release, and autorelease. Apple does this with Core Foundation types (though it's not exactly documented).

1
votes

The full text is:

A retainable object pointer is either a null pointer or a pointer to a valid object. Furthermore, if it has block pointer type and is not null then it must actually be a pointer to a block object, and if it has Class type (possibly protocol-qualified) then it must actually be a pointer to a class object. Otherwise ARC does not enforce the Objective-C type system as long as the implementing methods follow the signature of the static type. It is undefined behavior if ARC is exposed to an invalid pointer.

The English syntax is a bit convoluted, but it says that if a pointer is not declared as a Class then ARC does not do anything with it.