Make Swift Playgrounds Apps: Using Functions

Transcript

We have Identifiers for values we can use  repeatedly.  If you think of  constants as nouns we’ll need some verbs to do actions. Swift has actions we can use repeatedly. 

We call these  functions. Here’s a simple example. Add this to your code just below the thing2 declaration.  

let myMax = "\(max(thing,thing2))"

This is a function. A function is a set of code stored somewhere that does something you want done. In this case, the max function finds which number of two numbers is bigger.  

Functions require values to work correctly. You’ll find two ways function obtain values. The max function has in parentheses thing and thing2, We call stuff in the parentheses parameters.  

We’ll make the result on the bottom of our output. Add this to our code. 

Text(myMax)

We’ll see in preview that we get our answer of 11. Text also had parentheses – is Text also a function?

Before I answer that question, let’s look at a string function first, and show you an example of the second and more common way you might use a function in Swift. Change the code to 

Text(myMax.appending(" is bigger"))

If you look at the preview, swift Added  is bigger to 11. In some computer languages, you’d see both myMax and ” is bigger” as parameters, much like max up above. However in object oriented languages, you’ll often find one parameter outside the parentheses , then a dot, and then the function with any parameter it needs. 

When a function takes this form we call it a method. Methods are functions that require a special data type to run. Identifiers assigned to a data type are called objectsmyMax is an object of the String type, and appending is a method of String. Types in swift are usually capitalized. 

So if you go back to Text, is it an object or a type?  Text is a type defined by our user interface framework called SwiftUI. All types have a hidden method called init. The init method sets up a new object of a Type. So our code really reads.

Text.init(myMax.appending(" is bigger")) 

A Text object with the results of  myMax and  ” is bigger “ gets created then used immediately. 

There’s another object called Spacer, which makes variable spacing. If I place it under the code we have I can move everything to the top.  Using Init I’d write it like this. 

Spacer.init()

Because there would be Init everywhere,  in Swift and SwiftUI, we hide init by specifying a type and the parameters of initSpacer had no parameter but you always have to show the parentheses so I get. 

Spacer()

If I hide init.

Apple created a framework called SwiftUI to create user interfaces for your applications in Swift. We’ve been writing in SwiftUI code. Text, Image, VStack, and Spacer are all SwiftUI code. SwftUI has special function it adds to object called modifiers.  You see some modifiers on the Image Object such as imageScale, and foreground color.  There’s also a modifier called bold which works on text.  Add that to our text. 

Text(myMax.appending(" is bigger")).bold()

The text changes to bold. Modifiers change the properties of the object.There is also a font modifier.  I’ll make Hello Pizza use a large title and bold. 

Text("Hello, Pizza!").font(.largeTitle).bold()

And we have a bigger font in bold. 

I’ll change the Image too to use a title font. 

Image(systemName: "\(thing).square")
    .imageScale(.large)
    .foregroundColor(.accentColor)
    .font(.title)

The foregroundColor change the color of text and Sf Symbols. I’ll make it red. Notice this has some predefined values. We can select one of these fro easy color choices. 

Image(systemName: "\(thing).square")
    .imageScale(.large)
    .foregroundColor(.red)
    .font(.title)

            With a few functions, methods and modifiers we’ve changed the look of our app quite a lot. We’ll make some more changes next time as we add some more objects and  Learn to arrange the SwiftUI Objects we have. 

The Whole Code

import SwiftUI

struct ContentView: View {
    let thing = 5 + 3 * 2
    var body: some View {
        let thing2 = thing + 5
        let myMax = "\(max(thing, thing2))"
        VStack {
            Text("Hello, Pizza!").font(.largeTitle).bold()
            Image(systemName: "\(thing).square")
                .imageScale(.large)
                .foregroundColor(.red)
                .font(.title)
            Text("Hello, \(thing2)!")
            Text(myMax.appending(" is bigger")).bold()
            Spacer()
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: