This doesn’t cause any changes to the execution of the scripts, but it makes Xcode more aware of the files you will change or use in your scripts. Xcrun -sdk macosx swiftc -parse-as-library $SCRIPT_INPUT_FILE_0 \ In the run phase you added, add to the Input Files list $(SRCROOT)/Scripts/HelloXcode.swift and update the script to: This way, your scripts can be more dynamic and portable to use. Think of it as sending a file as a parameter to a function. Xcode’s run phase allows you to specify files as configuration instead of having them explicit in the script. You’ll see the message you printed logged directly in Xcode’s log. The difference between this and what you did before on the terminal is that Xcode executes these scripts on the path of the project file - that’s why adding Scripts/ is important.īuild the project to try out the new script and build phase you just added, then open the build log when it finishes. Xcrun -sdk macosx swiftc -parse-as-library Scripts/HelloXcode.swift \ Going back to the new phase you added a moment ago, delete the commented line and add these commands: For this tutorial, you’ll add new operations to the beginning - so when you add a new run script phase, drag it to the top of the list. However, you may want to add your own operations at specific moments. Links the compiled files with their compiled dependencies.Identifies the dependencies and compiles them.When you build any project, Xcode does a number of steps in order: The Build Phases tab is the central point for Xcode’s build operation. Then in Build Phases add a new phase and select New Run Script Phase from the drop-down menu. All you need to do is define a New Run Script Phase and add the commands you executed on the terminal into it.įrom the Project navigator, select the project file. The output file will be named CompiledScript.Ĭompiling your Swift file and executing it from Xcode is as easy as doing it from the terminal. This command compiles HelloXcode.swift using the macOS SDK. Xcrun -sdk macosx swiftc -parse-as-library HelloXcode.swift -o CompiledScript You can also rename the output file to something specific using the -o attribute. Ideally, since you’re going to execute them from Xcode, it makes sense to use the macOS SDK. You can also specify what kind of SDK you want to compile the code against. Xcrun can take several parameters one of them is -parse-as-librarywhich specifies to treat this file as a library otherwise, it’ll complain about the attribute. If you double-click the compiled file, it’ll open a new terminal window with some more messages before and after Hello Xcode. You compiled this application and executed it. You just created a very basic application that does nothing except print the text Hello Xcode. This will print Hello Xcode in your terminal window. This will compile your Swift file and create an executable binary with the same name. For example cd /Users/your_user_name/SwiftBuildPhase/Starter/Scripts/ Note: To navigate to a path on the terminal, use the command cd followed by the path you want to reach. Xcrun swiftc -parse-as-library HelloXcode.swift Then, open the terminal, navigate to the Scripts folder and execute this command: The code above prints “Hello Xcode” in the console. Writing Hello XcodeĬreate a new Swift file under Scripts and name it HelloXcode.swift. You’ll see a view controller with some text:įor this tutorial, you’ll work in the Scripts folder. Unzip it and open HelloXcode.xcodeproj in the starter folder. Loading remote data and interrupting the build processĭownload the starter project by clicking the Download Materials button at the top or bottom of the tutorial.Executing command-line operations from your code.Altering project resources during build time.Reading the project’s settings and adding custom values yourself. Configuring input and output files for scripts.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |