Swift Learning

Location Services

There are three types of location services

  1. Visits location
  2. Significant Change
  3. Standard

The first two don’t rely on GPS location and are power-efficient. They are not meant for real-time applications or precision. Every location service needs authorisation.

Authorization

Apple Developer Documentation Add the required Description to the Info.plist file. The description does not need to contain the App’s name. It should be friendly in tone and possibly give an example why the data is needed.

In the code, create a user dialog and ask for permission with CLLocationManager().requestWhenInUseAuthorization.

Standard location service

Create a Class implementing NSObject, CLLocationManagerDelegate. NSObject is required. I am not sure at the moment why, but what I read is that the underlying Objective-C of the Apple-provided imports needs this declaration as well. Link the delegation handler to an instance of CLLocationManager. clman.delegate = myDelegatedInstance

Swift Language

Attributes

Attributes have the @ operator. Attributes provide additional information about a declaration or type. A widely used case for an attribute is the @State in SwiftUI.

Delegation

Delegation is a design pattern to hand off responsibilities to an instance of another class. To use this pattern, a few steps are necessary:

Define a protocol

protocol MyDelegate {
    func myfunc()
}

Use this protocol for an assignment in another Class

class MyClass {
   var delegate:MyDelegate?
}

So delegate is ready to receive any class that conforms to the MyDelegate protocol. Now implement this Class

class MyDelegationClass: MyDelegate {
    func myfunc() {
        // function implementation
    }
}

The delegation can be linked with the calling Class

let caller = MyClass()
let delegater = MyDelegationClass()
caller.delegate = delegater

Opaque Type

The Opaque Type was introduced in Swift 5.1 and comes into play whenever the concrete type is unknown but conforms to the protocol or protocol composition.

Optional

As in Rust, Swift knows Optionals that are enums consisting of a none and a some type. It is used if the presence of a variable is optional.

# Declare Optional
var optInt: Int? # Short-hand declaration
var optInt: Optional<Int> # Complete declaration

An Optional can be unwrapped with the unsafe short-hand optInt! which results in runtime error if it can’t be unwrapped, for example if the variable contains nil. The complete declaration is Optional<optInt>

Variables

A constant value is declared with let whereas variables are declared with var.

SwiftUI

To describe a view, structs are used. Swift uses modifiers for structs which can programatically change them, which in turn changes the layout. Updates happen asynchronously. States will not always propagate immediately.

struct ContentView: View {
    var body: some View {}
}

The struct implements the protocol View with the required variable body.

some declares an Opaque Type. The structure View can have as many different implementations as there are layouts. That’s why a some is given, so that any structure conforming to the View protocol can be used.

@State Attribute

The State attribute is used for variables that define the single source of truth for a state in a _View_. A State property should only be accessed by the View itself or accessed by a function that’s called from inside the View. Calling states is thread safe. Whenever a State variable changes, the body of the View is recomputed. A State is a means of reading and writing a value and is not the value itself. Projecting a value works by prepending a $. It will pass the value to another View.

WindowGroup

The WindowGroup is a container. It can contain more than one View. It declares a hierarchy of Views. Each Window allocates its own State. Depending on the platform, the WindowGroup can allow to present the different Windows simultaneously, for example on macOS and iPadOS.


References