Sorry this is my first project in Swift3 and I hit lots of newbie questions (I come from C :)
I am trying to create a variable to store the properties for a platform that runs on an Arduino. This code interacts with the Arduino via BLE.
The platform is made of boards (up to 4) which contain modules (up to 4). Each structure has properties and I want to be able to change their values during the execution of the code.
The code below doesn't compile --> Error: "Class 'ViewController' has no initializers".
I understand that my structure declaration is lacking definition (ex. how many boards or modules are declared by default) but I can't see how to make it work.
Here are my questions:
Does someone know how to make it work with minimal changes? (I wouldn't want to switch to a class, because I understand less how they work...)
I read the Swift [Initialization][1] page and most of their examples use 'let' instead of 'var'. If I use 'let' for the declaration I can't change the property values later in the code... am I missing something in the use of variable?
Thank you!
in ViewController.swift:
class ViewController: UIViewController
{
struct moduleStruct
{
var address: UInt8 = 0
var status: UInt8 = 0
var settings: [UInt8] = [0,0,0,0,0,0];
var temp: Double = 0.0
}
struct boardStruct
{
var type: UInt8 = 0
var module: [moduleStruct]
}
struct platformStruct
{
var firmwareRevision: String = "0.0"
var board: [boardStruct]
}
var myPlatform : platformStruct
override func viewDidLoad()
{
super.viewDidLoad()
...
// example of use
myPlatform.board[1].module[0].type = 3
...
}
=== 4/3/17 EDIT - WORKING SOLUTION ===
struct moduleStruct
{
var address: UInt8 = 0
var status: UInt8 = 0
var settings: [UInt8] = [0,0,0,0,0,0];
var temp: Float = 0.0
init () {}
}
struct boardStruct
{
var type: UInt8 = 0
var module : [moduleStruct] = []
init ()
{
let emptyModule = moduleStruct()
self.module.append(emptyModule)
self.module.append(emptyModule)
self.module.append(emptyModule)
self.module.append(emptyModule)
}
}
struct platformStruct
{
var firmwareRevision: String = "0.0"
var board : [boardStruct] = []
init ()
{
let emptyBoardStruct = boardStruct()
self.board.append(emptyBoardStruct)
self.board.append(emptyBoardStruct)
self.board.append(emptyBoardStruct)
self.board.append(emptyBoardStruct)
}
}
var myPlatform : platformStruct = platformStruct()
override func viewDidLoad()
{
super.viewDidLoad()
...
// example of use
myPlatform.board[1].module[0].type = 3
...
}