Download Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets - Programming Ebook

Latest

Download Programming Ebook

Monday, February 18, 2019

Download Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets

Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets
Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets



Book Details 
             TitleSwift 4 Recipes
      ISBN-10978-1484241813
         AuthorYanis Zafirópulos
    Language: English
        SubjectSwift / Computers & Technology / Programming / Apple Programming
No. of pages: 371
         Format: PDF, EPUB


CHAPTER 1
Introduction

Now that you have gotten your hands on this little book, let’s get them a bit... dirty. And what’s better than delving into our first program?

In this chapter, we’ll see how we can write our first program in Swift, compile it, run it, and even see how Swift can be used as a scripting language.

1-1. Getting Familiar with Swift Problem

You’d like to execute and compile a simple Swift program that you just created
.
Solution

Fire up your favorite editor, and let’s write this very basic one line of code.
print("Hello Swift!")

How It Works

Now that we already have our code file, let’s save this as hello.swift. The next rather logical question is: How do we actually run this thing?

Executing It
Well, you might be expecting something along the lines of “Open Xcode, etc, etc,” but - since I’m rather old-school and prefer having total control over what goes on, why/how it is executed, and so on - what could be better than doing it straight from the command line?

So, just open the Terminal app and let’s go to the location where your hello.swift resides. Now, all that’s left – provided that you already have Xcode installed (and the accompanying developer tools) – is running our script:
swift hello.swift
And here is the output:
Hello Swift!
That was easy, wasn’t it? Pretty much like with any scripting language (see PHP, Python, Ruby, Perl, etc.). But wait: Is Swift a scripting language? Well, not quite...

Compiling It

What if we actually want to compile this code of ours and have a binary, which we can then use, without having to recompile or keep the Swift compiler around?
Let’s see...
swiftc hello.swift
Super easy again, nope? Just note the use of swiftc (the Swift compiler) instead of swift.
Now, if you look again into your folder contents, you’ll notice a single hello binary, which you can execute, like any normal binary:
./hello
And that was it. 4

1-2. Scripting in Swift Problem

You’d like to use Swift for Scripting, pretty much like any regular interpreted language, such as PHP, Python, Ruby, Perl, or... even Bash. Well, here’s the catch: given its speed, it sure can: at least for smaller
scripts. Plus, since Apple has already launched it as open source software (already having been ported to Linux), this makes it a not-so Mac-only thing.
So, the answer is: Why not? Solution
Start with the same Hello World script we played with in the previous section.
But now, let’s add the magic shebang line: #!/usr/bin/env swift
What we do here with this magic initial line is to help our Terminal recognize our script as executable source code – otherwise, it’ll be treated as some random text: we basically tell Bash which compiler to use (Swift), and actually to look for it (/usr/bin/env).
#!/usr/bin/env swift
print("Hello Swift!")
We save it as hello_script.swift. And that was it.

How It Works

First, we make it “officially” executable by:
sudo chmod +x hello_script.swift

We can now execute it as we would execute any script:
./hello_script.swift
Yay! Now, it’s time to rock’n’roll! :)

1-3. Summary

In this chapter, we’ve seen how you can get started with Swift easily so that you’re able to write and execute your application.
In the next section, now that you have all the basic tools set up, we’ll begin digging into the basics of the Swift language and step-by- step explore all of the techniques that’ll turn you into a top-notch Swift programmer.

CHAPTER 2

Basics

In this chapter, we will look into some basic constructs and conventions of the Swift programming language and help you get started writing your first program.

2-1. Create a Hello World Problem

I want to write a Hello World program and write my first Swift program.

Solution

A single line does the trick. Hello World!
print("Hello World!")

How It Works

All we have to write is a single line of code, using the print function. Hello World!

2-2. Declare variables Problem

We want to declare some variables.

Solution

Declare a variable, without type.
var surname = "Doe"
Declare a variable, with type.
var fullName : String = "John Doe"
And another one ...
var age : Int = 25
Now, since it's a variable, why not change it?
age = 32
So, let’s see what we’ve done.
print("My name is \(surname). \(fullName). And I'm \(age) years
old.")

How It Works

Variables are at the very core of every program. Basically, they consist of an identifier (symbolic name) associated with a value, which - in contrast with constants - can change.
My name is Doe. John Doe. And I'm 32 years old.

2-3. Declare constants Problem

We want to declare some constants.

Solution

Declare a constant, without type.
let surname = "Doe"
Declare a constant, with type.
let fullName : String = "John Doe"
And another one.
let age : Int = 32
If we attempt to reassign any of the above, like: age = 31
the compiler will complain. So, just don’t do it.
If you want to be able to change them, just use variables, not constants. So, let’s see what we’ve done...

print("My name is \(surname). \(fullName). And I'm \(age) years
old.")

How It works

A Constant is a value that - in contrast to variables - cannot be altered by the program during normal execution.
My name is Doe. John Doe. And I'm 32 years old.

2-4. Declare multiple variables Problem

I want to declare multiple variables.

Solution

var red, green, blue : Double
or even assign all of them on the same line:
var name = "John", surname = "Doe"
So... what’s my name?
print("My name is \(name) \(surname)! Not really...")

How It Works

You can declare multiple variables of the same type, in the same line. My name is John Doe! Not really...

2-5. Declare Unicode constants and variables

Problem

AI want to declare constants and variables with Unicode names.


Solution

So, setting a constant this way is absolutely correct Swift code
let π = 3.14159
Or, even this one - now, let’s set a variable.
var 好好 = "ni hao" Let’s use our variable.
print("'Hello' in Mandarin Chinese = \(好好)") How It Works

In Swift, constant and variable names can contain almost any character, including Unicode characters.
'Hello' in Mandarin Chinese = ni hao

All Books From Ray Wenderlich, Bundle 10 Books


No comments:

Post a Comment