Common blocks in Swift?

I am porting an old app from ObjC. The app uses many defined constants such as:

#define COM_OFFSET  12.5

and many variables that are read and/or written throughout the App, such as:

PCDate* Dates[367];

@class PCMainView;
PCMainView* MainView;

in one file called "PCCommon.h"

How do I duplicate this function in Swift? I have looked around and have found no help.

Thanks in advance.

Answered by darkpaw in 818306022

#define COM_OFFSET 12.5 is simply let COM_OFFSET = 12.5.

It looks like you want a file of commonly-used values/constants. That's easy. Just create a new Swift file and put your declarations inside, e.g.:

let COM_OFFSET = 12.5

// You don't have to give arrays an initial size, but don't make them a `let` because you can't then add anything to it
// So, either create an empty array globally like this, and add entries somewhere else, like:
var dates: [PCDate] = []
addToDates()

func addToDates() {
    dates.append(Date.now)
    dates.append(Date.now.advanced(by: 60)
}

// Or you can create a function that populates it when you create it, e.g.
var dates = initialiseDates()

func initialiseDates() -> [Date] {
	var array: [Date] = []
	for index in 1...3 {
		array.append(Date.now)
	}
	return array
}

For the class stuff, just read up on how to create classes in Swift, but it's very easy - and you should put classes in their own file, so:

import Foundation

class MyLogger {
	static let fileURL: URL = {
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "my.app.group")!.appendingPathComponent("main.log")
	}()
...

Then you just create an instance of it like any other class, just like you did with var dates: [Date] = [] above, i.e. var logger: MyLogger.

Accepted Answer

#define COM_OFFSET 12.5 is simply let COM_OFFSET = 12.5.

It looks like you want a file of commonly-used values/constants. That's easy. Just create a new Swift file and put your declarations inside, e.g.:

let COM_OFFSET = 12.5

// You don't have to give arrays an initial size, but don't make them a `let` because you can't then add anything to it
// So, either create an empty array globally like this, and add entries somewhere else, like:
var dates: [PCDate] = []
addToDates()

func addToDates() {
    dates.append(Date.now)
    dates.append(Date.now.advanced(by: 60)
}

// Or you can create a function that populates it when you create it, e.g.
var dates = initialiseDates()

func initialiseDates() -> [Date] {
	var array: [Date] = []
	for index in 1...3 {
		array.append(Date.now)
	}
	return array
}

For the class stuff, just read up on how to create classes in Swift, but it's very easy - and you should put classes in their own file, so:

import Foundation

class MyLogger {
	static let fileURL: URL = {
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "my.app.group")!.appendingPathComponent("main.log")
	}()
...

Then you just create an instance of it like any other class, just like you did with var dates: [Date] = [] above, i.e. var logger: MyLogger.

Common blocks in Swift?
 
 
Q