@carlogilmar

Blog for share my learning experience as software developer since 2016.

Learning Swift

Learning Swift

Recently I have been studying about Swift. I think that the learning experience is a wonderful resource for other persons who are in the same situation, so I consider valuable talk about this.

First Steps

When I start to learn swift I decide to watch videos and demonstrations about how to use the Xcode for build iOS apps, but, for be honest, I couldn’t understand some things: optionals, constrains, auto layout… This was my first experience. So I decide start with basics.

I considered that know and understand the language is important, and Swift is a language with a lot of features. The plan was know more about it, and then introduce me to iOS apps development.

Installation

By default you have swift installed in you MacOs, but you can install Swift in a linux SO: Installing Swift Oficial Website

My first professional language is groovy, so was normal for me find some concepts in this new language. The first things that I found about swift were:

  • Xcode for build iOS apps
  • Playgrounds

But this aren’t the only tools!

Meeting Swift in Command Line

I think that use the command line is better for developer life, fortunately swift is a scripting language, and it have a REPL. I found it more useful in this part of my life instead of use only playgrounds, although this are visual and have a great visual debugger, also are slow in some cases.

You can write some like this:

print("Hello World from this swift script! 😎")

Save as hello.swift and run with swift hello.swift

If you have a lot of warnings in your REPL, you have to configure your Xcode Command Lines. Go to Xcode > Preferences > Command Line Tools and select your Xcode version

Also swift have a own package for build apps. Remember that swift is general-purpose programming language, so you can build web apps too using Vapor Swift Frameworks for example.

This package manager is useful in the command line:

mkdir sample-app && cd sample-app

swift package init

 ~/D/sample-app >>> swift package init
Creating library package: sample-app
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/sample-app/sample_app.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/sample-appTests/
Creating Tests/sample-appTests/sample_appTests.swift
Creating Tests/sample-appTests/XCTestManifests.swift

We could make the follow things:

  • Test this project with swift package test
  • Built this project with swift build
  • Create the Xcode project for use it swift generate-xcodeproj
  • Use the LLDB Debugger

A text editor for Swift

You can use your own text editor! I use VIM in my day by day, and I’m in love because it’s a useful tool for me. So I decide to use it with this plugin for swift syntax Swift VIM Plugin.

The language

The swift language is elegant. It’s a dynamic language, so it could be typed or inferred. Sometimes looks like C, and you can implement some programming approaches as structured, oriented programming and functional programming, have pattern matching, protocols extensions, generics, and more features.

I found interesting make some like this:

struct Student{
    var name: String
    var age: Int
}

struct ListOfSomething<T>{

    var list:[T] = []

    mutating func setList(elementList:[T]){
        self.list = elementList
    }

    func countElements() -> Int{
        return self.list.count
    }
}

// A list of students
var student1 = Student(name: "carlo", age: 20)
var student2 = Student(name: "gilmar", age: 21)
var student3 = Student(name: "carlogilmar", age: 22)
var studentsList:[Student] = [student1, student2, student3]

// A list of something of students
var buffer = ListOfSomething<Student>()
buffer.setList(elementList: studentsList)
buffer.countElements()

My Resources for learn

For understand this language I try to start with the basics, so I use the follow books for know more about swift:

Now, I’m learning about iOS development, and I found some interesting resources:

This is my learning experience as beginner learning Swift. I hope to help you with start you own way of learn.

Thanks for read it!


comments powered by Disqus