Swift 4 Tutorial-1 [Variables and Constants]

in #utopian-io6 years ago (edited)

What Will I Learn?

In this article series we are going to start programming iOS. I am going to try to teach you how to code with Swift, how to write apps for iOS and how to develop sample apps from scratch by working on them together. This caption is the first series of “mobile app development with Swift” articles and the series will continue.

p1.png

Requirements

  • An apple device for swift programming (Mac, Macbook Pro etc.)
  • Xcode
  • Determination and persistence

Difficulty

  • Basic

Tutorial Contents

Constant: Once you assign a value for a constant, there is no turning back in Swift programming language.

Variable: You can change a value of a constant whenever you want.

Constants and Variables should be defined before use. Let keyword is used to define a constant and var keyword is used to define a variable. For instance; Type Annotation. Type Annotation is used for defining which type of a value can be held by a constant or a variable.

Naming Constants and Variables is probably the funniest part of Swift programming language. We are able to use all characters when we are naming them.

Variable types at Swift 4: String - İnteger - Double

Let keyword is used to define a constant;
Var keyword is used to define a variable.

var m = 4 
print(m) .  // var is used if it is considered as a variable.

let π = 3.14 
print(π) .  // // let is considered as a constant value.

What is String?

var emptyString = ""            // Empty (Mutable) String
let stillEmpty = String()       // Another empty String
let helloUtopian = "Hello Utopian!" // String literal
let a = String(true)            // from boolean: "true"
let b: Character = "U"          // Explicit type to create a Character
let c = String(b)               // from character "A"
let d = String(3.14)            // from Double "3.14"
let e = String(1000)            // from Int "1000"
let f = "Result = \(d)"         // Interpolation "Result = 3.14"
let g = "\u{2126}"              // Unicode Ohm sign Ω
  • Strings are data types which can be expressed by character series. The content of the Strings are made of Character values.
let dollarSign = "\u{24}"        // $,  Unicode scalar U+0024
let blackHeart = "\u{2665}"      // ♥,  Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // 💖, Unicode scalar U+1F496
  • We define Strings by defining a initial value in double inverted comma:
var userName = "Burak"
    
    userName.append("o")  // Adds a new element at the end of the array.    
    userName.capitalized // Returns a capitalized representation of the receiver using the specified locale.
    userName.lowercased()  // Returns a lowercase version of the string.
    userName.uppercased()  // Returns an uppercase version of the string.

\n is used as ‘return’ while we are saying that; a sequence of characters surrounded by three double quotation marks. You wrap the strings in triple double quotes.

let swift = """
               Welcome to   
               Swift 4
              training
    """
    print(swift)

Empty String

  • We can use one of those methods in order to give an empty string to a string as an initial value:
var emptyString = ""               // empty string
var anotherEmptyString = String()  // initializer syntax

// both strings have empty string values.

We can understand if a string is empty or not by using isEmpty property:

if emptyString.isEmpty {
    print("Nothing to display")  // Output is as you see below “Nothing to display”
    }

Combining Strings

let string3 = "I Love "
let string4 = "You"
    
var result = string3 + string4

Combine a value by using +- expression

var instruction = "Darling "
    instruction += string3

To separate a String expression into its arrays:

print(Array(result))

String Comparing (Comparing) - We can compare Strings by using (==) and (!=):

let str1 = “Learning with Swift 4”
    let str2 = "Learning with Swift 4”
    if str1 == str2 {
    print(“These two strings are equal to each other")   
// Output is as you see below "These two strings are equal to each other”
    } /* else {
    print(“Not equal :)")
    } */

if str1 != str2 {
    print(“These two strings are not equal to each other”)
    }

Counting Characters

To retrieve a count of the Character values in a string, use the count property of the string.

var word = "utopian"
print("the number of characters in \(word) is \(word.count)")
// Prints "the number of characters in utopian is 7"

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Hi, these are the following reasons your contribution was rejected

  • The linked repository is wrong. If you create a GitHub repository with additional material (like code samples), make sure to choose the repository of the project your tutorial is about and not your own repository. You can provide links to your repository in your post.
  • Tutorials about basic programming concepts (variables, operators, loops, etc.) will not be accepted.

I recommend you read the rules before contributing again in the future; make sure the next one is less trivial and select the correct repository.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.030
BTC 60270.38
ETH 3307.79
USDT 1.00
SBD 2.40