Why is __bridge not needed for constants from frameworks?

Today I discovered that constants from frameworks aren't supposed to have __bridge

a cast statement. How:

NSString *cast = (NSString *) kUTTypeMovie;

      

Where

extern const CFStringRef kUTTypeMovie __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0);

      

I researched this opinion, what const

might affect it and no luck. These two require a __bridge

cast:

const CFStringRef cf_const;
NSString *ns_const = (NSString *) cf_const; // ARC bridge cast is required

CFStringRef cf_nonconst;
NSString *ns_nonconst = (NSString *) cf_nonconst;  // ARC bridge cast is required

      

So can someone explain this?

+3


source to share


2 answers


When you see const CFStringRef it is a constant reference to a string, which means that you have a variable that contains that reference and you are not allowed to assign that variable. It doesn't mean anything about CFStringRef itself.



0


source


An interesting observation.

The case you find is handled specifically by the compiler. Details can be found in the Type Conversion of Persistent Pointer Objects with Known Semantics in the Clang documentation. It says:

Bridge lists are annoying. The lack of ability to fully automate the management of CF objects, however we are left with relatively poor attempts to reduce the need for excess explicit bridges. Hence these are the rules.

[...] For loads of persistent pointer type C constant global variables, it is reasonable to assume that global system constants were initialized with true constants (such as string literals), but custom constants could be initialized with something dynamically allocated using a global initializer.



The above assumption is based on the coding techniques used by the system (framework), but it is not possible to define that user-defined globals should follow the same practices, so an explicit bridge relocation is required.

NTN

0


source







All Articles