I have a struct (someUrl) containing 3 static vars:
struct someUrl {
static var keywords = String()
static var someUrlStart = String()
static var someUrlEnd = String()
}
and a class (Url) with a variable of type someUrl:
class Url {
var someUrlConstructor: someUrl
init(keywords: String, someUrlEnd: String, someUrlStart: String) {
self.someUrlConstructor = someUrl(someUrlStart = someUrlStart, keywords = keywords, someUrlEnd = someUrlEnd)
}
}
In trying to initialize the variable in the class, Xcode is throwing an error stating that someUrlStart is a let constant.
cannot assign to value: 'someUrlStart' is a 'let' constant
This is ALL I have in my code so far, and I don't get why a static var would be treated as a constant. Is this specific to initializers?
You are confusing the compiler here. First your syntax is wrong in terms of how you pass parameters, it should be :
instead of =
, then you should remove the static
and change the order of the parameters:
struct someUrl {
var keywords = String()
var someUrlStart = String()
var someUrlEnd = String()
}
class Url {
var someUrlConstructor: someUrl
init(keywords: String, someUrlEnd: String, someUrlStart: String) {
self.someUrlConstructor = someUrl(keywords: keywords, someUrlStart: someUrlStart, someUrlEnd: someUrlEnd)
}
}
Alternatively if you want to keep them static remove the parameters completely since now they are static variables and not member / instance variables:
struct someUrl {
static var keywords = String()
static var someUrlStart = String()
static var someUrlEnd = String()
}
class Url {
var someUrlConstructor: someUrl
init(keywords: String, someUrlEnd: String, someUrlStart: String) {
self.someUrlConstructor = someUrl()
}
}
What the compiler thought you were doing (or basically what you were in fact doing writing =
) was trying to change the someUrlEnd
(and the other two) you were given as a initializer parameter which is in fact a constant.
Apart from the wrong syntax I do not see a use for the static
or for the class Url
, just take the struct someUrl
, its default initializer and go from there. General note: please upper case the first letter of the struct: SomeUrl
.