Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift Cookbook
  • Table Of Contents Toc
Swift Cookbook

Swift Cookbook

By : Costa
5 (1)
close
close
Swift Cookbook

Swift Cookbook

5 (1)
By: Costa

Overview of this book

If you are an experienced Objective-C programmer and are looking for quick solutions to many different coding tasks in Swift, then this book is for you. You are expected to have development experience, though not necessarily with Swift.
Table of Contents (13 chapters)
close
close
12
Index

Using Swift as an interpreter

Like some other script languages, you can use Swift with its interpreter on the command line. Sometimes it's very useful, mainly when you want to test code but you don't want to create a new playground.

Getting ready

Open a terminal window as it was shown in the previous recipe.

How to do it...

Follow these steps for using Swift as a command line interpreter:

  1. The first step is to find where the Swift command is; even if you have used the xcode-select command, it is possible that the Swift command is not accessible from your PATH variable. So, you can localize your Swift command using find /Applications/Xcode.app -name swift -a -type f. In my case, I got /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift. However, current versions of Xcode have the Swift command at /usr/bin. If it is necessary, add this directory to your PATH variable with the command export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/". At this moment, we can enter into the Swift interpreter just typing swift.
  2. If you want to use Swift from the command line, sometimes, it's a good idea to have this PATH variable set permanently. To make this, we need to add the previous command into our .profile file, such as echo 'export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/" ' >> $HOME/.profile && chmod +x $HOME/.profile. From now on, if you restart your computer, it won't be necessary to look for the Swift path and set the PATH environment variable again.
  3. Now, let's enter into our Swift command line and type the following code:
    var dividend = [3,2,1,0]
    var divisor = 6
  4. You will see a message showing the content of these variables after typing each of them. Now, type the following loop code:
    for i in dividend { 
          println("\(divisor) / \(i) = \(divisor / i)")  
    }
  5. Now, you can see that we will receive the following result:
    6 / 3 = 2
    6 / 2 = 3
    6 / 1 = 6
    Execution interrupted. Enter Swift code to recover and continue.
    Enter LLDB commands to investigate (type :help for assistance.)
  6. As you can see, the last option failed because we can't divide by 0 and that's a fast way we can test some code, using the command line. Most of the time, we will test using a playground, but sometimes using the command line is much faster.

How it works...

Calling the Swift command gives you the possibility to test your code or even use Swift as a scripting language. The highlight here is that you need to know where your Swift command is; the command line helps you to find it.

There's more...

Most of Swift's options and swiftc options are common; it means that if there is something that you would like to test before compiling, you can do it.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Swift Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon