Documentation of the Needle dependency injection framework sucks, and most of articles teaching you how to use it are outdated. That's the reason why it's really hard to write even a minimal-working version of the code that compiles.
This is a simple example of RootComponent
that compiles successfully. Based on a code from an article "Dependency Injection with Needle", but modified to make it work.
- Go to your target
- Build Phases
- Click "+" button
- New Run Script Phase
Move created build phase to very top of your list of build phases.
It's important to make sure "Shell" field containts /bin/sh
, and into a field below add following bash script:
export SOURCEKIT_LOGGING=0 && /opt/homebrew/bin/needle generate $SRCROOT/$TARGETNAME/NeedleGenerated.swift $SRCROOT/$TARGETNAME
Note that a path to the needle
binary should be absolute. You can locate this path with whereis needle
command.
![Screenshot 2024-03-10 at 15 34 14](https://private-user-images.githubusercontent.com/12474739/311509839-c27cfb6f-3cb8-4801-9643-d419b069586a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAyMDQ1MTMsIm5iZiI6MTcyMDIwNDIxMywicGF0aCI6Ii8xMjQ3NDczOS8zMTE1MDk4MzktYzI3Y2ZiNmYtM2NiOC00ODAxLTk2NDMtZDQxOWIwNjk1ODZhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA1VDE4MzAxM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWU1ODhhYTUwMWI3ZTk5ODNhZTZmNmJiNmRhNjRkZGE4OGQwZDUwZjA2NmNjZWY5OTI5ZWM4NmUwNTgzYzE1OTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.5gUFfBA8FMvDAg3GFOfb1KMDTNtE__EeVAUBhwOSDe8)
One more thing to do:
- Go to your target
- Build Settings
- Type "sandboxing" into a search field
- Set "No" to "User Script Sandboxing" build option
With user script sandboxing enabled Xcode won't allow needle
binary to read your project files and modify them.
![Screenshot 2024-03-10 at 15 41 21](https://private-user-images.githubusercontent.com/12474739/311510289-76b668a4-9eec-49f1-962c-64cf47272494.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAyMDQ1MTMsIm5iZiI6MTcyMDIwNDIxMywicGF0aCI6Ii8xMjQ3NDczOS8zMTE1MTAyODktNzZiNjY4YTQtOWVlYy00OWYxLTk2MmMtNjRjZjQ3MjcyNDk0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA1VDE4MzAxM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTZhMGU5NDVlMzEwZDQ0NTgxYmM5MTQ0NWQyZDM0MjQxNjRjYjNkNzA5NzQwMTZjMWJlMDY2NmE0YjQ1MTBiMmYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pqpCaG0wCKCDK4qQFIH8j4OgiZn9FmukRuCKPrOJdYc)
After you try to build a project NeedleGenerated.swift
file will be generated. Add it to a project via menu bar:
- File
- Add Files to $TARGETNAME...