How does CocoaPods add files to a target when they're actually not added as a member of a target?

59 views Asked by At

I'm working with a CocoaPod private library. We have mock files that are being used for our Swift Previews.

What's surprising is that these mock files aren't added as a member to the pod's target. They're a a member of the Test Target. Yet they compile.

I don't understand why/how it's working

1

There are 1 answers

0
mfaani On BEST ANSWER

When I further inspected the setup I noticed that within the PodSpec, for its source_files field I saw something like the following:

 # mock files are being built into the pod so they can be used for SwiftUI previews
 s.source_files = 'OurCoolPod/**/*.{swift,storyboard}', 'Example/Tests/Mocks/**/*.swift', 'Example/Tests/ManagerMocks/*Mock*.swift', 'Example/Tests/HelpersTestsAndMocks/*Mock*.swift', 

I believe the source files are added onto the target if either:

  • They're from within the above directories. pod install later adds them onto the 'Build Phase' of target under 'Compile Sources'
  • They're added to the correct target, but isn't added to a source_files paths. ATTENTION: things work only up until you do a pod install. As soon as you do pod install it will stop working because pod install will remove it from the Build Phase. Hence this isn't something you should do or rely on. I just mentioned it as insight for debugging things.

Tldr the PodSpec will supercede your Xcode configurations because pod install overrides things.

But why is CocoaPods like this?

I suppose mainly because it wants to have a higher focus on the Swift/Objective-C language and less focus on Xcode.

When you rely on paths instead of a very specific Xcode project file, then other tools like the terminal, vim, VSC can be used a lot easier.

Like you don't need Xcode running (you need the Xcode Command Line Tools installed though) to execute pod lib lint. And this in itself is a great advantage. It simplifies the development process.