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 …
- open Xcode via clicking its macOS icon
- click “Create a new Xcode project” button
- choose Application -> “Command Line Tool” then click “Next” button
- Project Name: “Swift Command Line”
- Language: “Swift” then click “Next” button
- in Project Navigator menu click “main” to see the default Swift code
- 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 - 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
- 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
- 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 ...
- open Terminal via clicking its macOS icon
- 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 ... - 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 ... - 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.