Embedded framework is not loaded if only category methods are used

Originator:an00na
Number:rdar://37317458 Date Originated:February 7 2018, 12:15 PM
Status:Closed Resolved:Behaves as Intended
Product:Developer Tools Product Version:9.2
Classification:Serious Bug Reproducible:Always
 
Summary:
1) App A embeds framework F, F contains some ObjC categories and other classes. If A *only* uses category methods of F,  "unrecognized selector" exception will happen at runtime. 

2) If A also uses any other classes of F then everything works fine.

3) If A not only embeds framework F but also explicitly links it by adding it in Linked Frameworks and Libraries in General Settings or Link Binary With Libraries in Build Phases, even if A only uses category methods of F, A can also run without issues.

By using OBJC_PRINT_IMAGES env var I can see that F is loaded in case 2 and 3 but not in case 1.

Because case 2 is the common case, wherein F is automatically linked and loaded without requiring developers to explicitly add it to Link section, developers will be very confused when case 1 occurs to them.

I think it is a genuine bug, especially considering that after F is added in Embedded Binaries section without also in Linked Frameworks and Libraries section(either because the project is created in old Xcode when this didn't automatically happen, or developers think it is not necessary and deleted it), F cannot be found again in the framework list popped by the + button.

Steps to Reproduce:
1. Run the sample app.
2. See it crash and read console logs.
3. Uncomment the [Foo new] line in AppDelegate.m.
4. Run again to see it works.
5. Comment out the [Foo new] line in AppDelegate.m.
6. Add F in Linked Frameworks and Libraries section.
7. Run again to see it works.

Code: https://mega.nz/#!9TA0wBCa!Db9LiHsHKJOlbctfb5gYH53pe8w0nKJsxrCFtk2FdjQ

Comments

Apple Developer Relations: February 21 2018, 3:33 PM

Finding Objective-C category dependencies is outside the goals/design of auto-linking. If you'd like, we can look at this in the future as a possible feature request.

Me, February 16 2018, 5:18 PM:

I get why this happens. But isn't "Objective-C categories cannot be caught by autolinking" a bug? It is definitely not what developers expect.

February 14 2018, 9:09 AM from Apple Developer Relations

This issue behaves as intended based on the following:

The compiler is behaving correctly here – Objective-C categories cannot be caught by autolinking. Autolinking is triggered by which symbols get used by the importing framework. Although FrameworkCategoryBug/AppDelegate.m does import the F framework, it does not use anything declared in its headers. If instead autolinking were based purely on the imported headers, it would pull in too many dependencies.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!