Xcode Command Line Project Executable Tutorial

Xcode Command Line Project Executable Tutorial

Xcode Command Line Project Executable Tutorial

Yesterday’s Xcode Swift Command Line Project Primer Tutorial combines with our recent macOS find related work, so that, today, we’ve written a small PHP “helper” web application, which “fishes out” that Xcode macOS executable, and executes it for you, that we want you to download to (the Document Root of) a macOS MAMP local Apache/PHP/MySql web server environment, “Intranet style”, as has also been a theme of our work recently.

What are the modes of use of this PHP “helper” web application? Well, any of …

  • surfing the web … via web browser URL like …
    HTTP://localhost:8888/find_executable_and_go.php?inproject=Swift%20Command%20Line&argvs=one+two+three
    … or fill out a form at find_executable_and_go.php‘s live run link
  • curl … via macOS command like …
    curl "HTTP://localhost:8888/find_executable_and_go.php?inproject=Swift%20Command%20Line&argvs=one+two+three"
  • PHP on command line … via macOS command like …

    php find_executable_and_go.php one two three

    … and fill out the Xcode project name

… or, after your downloading to macOS (where you’ve installed, and are using, Xcode), the ideas below could work …


Previous relevant Xcode Swift Command Line Project Primer Tutorial is shown below.

Xcode Swift Command Line Project Primer Tutorial

Xcode Swift Command Line Project Primer Tutorial

The macOS Xcode IDE (Integrated Development Language) is a great place to create many and various types of executable software. The language Swift is Apple’s language of choice, within Xcode, for …

  • iOS mobile apps … and today we build a simple …
  • macOS command line app

… which we allow to process your command line arguments. Then “we fish out” where Xcode has stored the resultant Swift command line executable, and execute that “binary”.

All told, we …

  1. open Xcode via clicking its macOS icon
  2. click “Create a new Xcode project” button
  3. choose Application -> “Command Line Tool” then click “Next” button
  4. Project Name: “Swift Command Line”
  5. Language: “Swift” then click “Next” button
  6. in Project Navigator menu click “main” to see the default Swift code
  7. we end up making our code be like main.swift

    //
    // main.swift
    // Swift Command Line
    //
    // Created by User on 10/4/22.
    //

    import Foundation

    var msg = ""
    var i = 2

    if CommandLine.arguments.count <= 1 { print("Hello, World!") } else { msg = CommandLine.arguments[1] while i < CommandLine.arguments.count { msg += " " + CommandLine.arguments[i] i += 1 } print(msg) }

    ... via good help from this link, thanks
  8. to execute in Xcode's Console we choose the menu option Product -> "Run" and see the results down the bottom to left in Xcode's Console with "Hello, World!" output because there are no command line arguments as the default condition
  9. to execute within Xcode with command line arguments ...
    • we choose the menu option Swift Command Line -> "Edit Schema..."
    • we choose the menu option Info -> Build Configuration -> "Release"
    • we click "Arguments" tab
    • we click "Arguments Passed on Launch" tab's "+" button ... and ...
    • type in "one two three four five" as our user specific command line arguments
    • click "Close" button
  10. we choose the menu option Product -> "Run" and see the results down the bottom to right in Xcode's Console with "one two three four five"

... and then (with thanks to advice of this link) we ...

  1. open Terminal via clicking its macOS icon
  2. type in the command (where, for us, /Users/user can equate to $HOME if you'd prefer) ...

    find /Users/user/Library/ -name "Swift Command Line" -exec file {} \; 2> /dev/null

    ... leading us to conclude that ...
  3. typing in ...

    "/Users/user/Library//Developer/Xcode/DerivedData/Swift_Command_Line-ayzqxnplmfgugrclirdeaxwmiryj/Build/Products/Release/Swift Command Line"

    ... would result in the output "Hello, World!" (as it did) ... and ...
  4. typing in ...

    "/Users/user/Library//Developer/Xcode/DerivedData/Swift_Command_Line-ayzqxnplmfgugrclirdeaxwmiryj/Build/Products/Release/Swift Command Line" one three five

    ... would result in the output "one three five" (as it did)

We hope this is of interest to beginners in Swift and Xcode IDE software development in macOS operating system.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>