My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Let's Dive a Little Deeper Into Swift!

Steven Wong's photo
Steven Wong
·Feb 24, 2020

It's been close to a month since I first wrote about Swift. It was simply an introduction into the language, and now that time has passed, it's time to learn a little bit more in-depth about the language.

Recap

So in my last post, "Swift! Let's Learn It Together!" we briefly touched on our development environments (mine has now changed to native macOS), variables and constants, optionals, and collections. These were very basic concepts to understand, but since I'm coming from a Java background, they were in-part, largely new to me. Well, I mean variables and arrays aren't new, but the way they're declared and used in Swift is. I mentioned that for the next blog post that we'd go a little more in-depth on the programming side of things, and then potentially talking about SwiftUI, so let us stop wasting time, and get into it!

Programming

In this area, I'm going to go over Object-Oriented Programming and Protocol Oriented Programming and trying to give an explanation in the differences between the two, Access Control, and some Language References.

Object-Oriented Programming (OOP) vs Protocol Oriented Programming (POP)

Swift is an interesting language. It is able to use both of these styles of programming just like some other languages like Python and Java 8. When reading and learning about this I came across a post on reddit by the user CodaFi who I think gives a great explanation of it. Going over it and trying to grasp the gist of it, and understand it, the laundry example was a great example. If you do not care to read the above link, or it happens to be blocked, essentially it something like this: Objects are like living breathing people. They have knowledge in them, and they have a memory so they can remember things. Except, we have to interact with them at a high level of abstraction. Let's say your clothes are dirty, and you need to get them cleaned. You send someone a message and tell them to clean them. That somebody knows where to get the clothes cleaned at the best place in the city, so they take your clothes and go out and get them cleaned. Then once they're cleaned, that somebody brings them back to you cleaned. But the thing is, with OOP, you have no idea where they were cleaned, maybe you don't even have money to pay for it. All that stuff is hidden inside of the person you messaged.

Protocols, on the other hand, they're essentially plans. They have the information on how to ask for things to get done and going back to the laundry example if we wanted to get them cleaned, they do the exact same thing as above, and they come back, but what if I didn't like the way the exchange went down? Well I can change it simply by asking someone else to do, and maybe they go and get it done for cheaper, or faster than the last guy, but again for some reason, I don't like this person, so I ask someone else and they do it exactly how I wanted it. Except, during this whole process we didn't have to know any of the information since the complexity of them getting your clothes cleaned was hidden inside of them.

Hopefully, I was able to get a general idea across to you, if not, I recommend watching this video by Apple from WWDC in 2015.

Access Control

This should be short and sweet, but it's something we will have to have a deeper understanding of going later into Swift so I figured it'd be a great point to touch on. Like Java, we have access levels for different entities within our code, they are the following:

  1. Open Access and Public Access enables entities to be used within a source file, or another source file that imports it.
  2. Internal Access enables entities to be used within any source file, but not outside of the source file.
  3. File-Private Access restricts the use of an entity to its own defining source file.
  4. Private Access restricts the use of an entity to the enclosing declaration, and extensions of that declaration in the same file.

As you can see, pretty similar to Java with their public, protected and private modifiers.

Language References

Just going to touch on some of the ones we are used to seeing from Java, but explaining the use of them in Swift!

Try Operator

The try operator has 3 types of expressions that can be applied to it.

try  // This is like our typical try-catch block in Java.
try?  // This is an optional-try expression. 
try! // This is a forced-try expression.

Loops

Note: Quotation marks are unnecessary, they're just placed for readability purposes.

For-In Loop:

for "item" in "collection" {
    "statements"
}

While Loop:

while "condition" {
    "statements"
}

Repeat-While Loop:

repeat {
    "statements"
} while "condition"

If Statements:

if "condition" {
    "statements"
}
// If-Else
if "condition" {
    "statements"
} else {
    "statements"
}

// If-Else-if-Else
if "condition 1" {
    "statements"
} else if "condition 2" {
    "statements"
} else {
    "statements"
}

Switch Statements:

switch "control expression" {
    case "pattern 1":
        "statements"
    case "pattern 2" where "condition":
        "statements"
    case "pattern 3" where "condition",
         "pattern 4" where "condition":
        "statements"
    default:
        "statements"
    }

A lot of these look like statements we're used to in Java, and use almost everyday within Java itself. So this should be a very easy transition into Swift itself.

Whats Next?

I would have liked to touch on SwiftUI, but I think that will have to be saved for the next blog post. I covered a lot in this post, and it was getting quite lengthy so unfortunately I didn't get to go into SwiftUI, but in all honesty that is probably something that could use its whole own post. But for now, we learn a little more of Swift, and start developing a small application! Until next time!