I have a class named AM_Template
From that I created a class variable named AM_line and put some values into the variables within the class. The initializer is an integer, i0. For example,
var AM_line = AM_Template(i0: 0)
AM_line.CallSign = “WABC” where CallSign is a variable in the class object AM_line
Then I created a class array
var AM: [AM_Template]
Now I want to append AM_line to AM
The statement I am using is …
AM.append(AM_line)
But I get an error and a suggestion.
The error is…
Value of optional type 'AM_Template?' must be unwrapped to a value of type 'AM_Template'
Selecting the first choice suggestion, the statement changes to …
AM.append(AM_line ?? <#default value#> )
When I am dealing with a class object, and when that class object requires an initial value (for example i0:0)
What do I put in place of <#default value#> to remove the error and make it work?
tera
2
Same suggestion here IRT naming and triple quoting with ```
Your's AM_Template type's init is "failable" (i.e. it is "init?(....)" instead of "init(...)")
Use one of these:
var defaultLine = AM_Template(i0: some good value)!
1. var line = AM_Template(i0: 0) ?? defaultLine
2. guard var line = AM_Template(i0: 0) else { ... } // ... for return or throw or fatalError()
3. if var line = AM_Template(i0: 0) {
... use line here
}
4. var line = AM_Template(i0: 0)! // will crash on nil