I'm working through a learn-swift playground and upgrading it to Swift 2.0 as I learn the language. The following code (which likely worked with prior versions of Swift) now generates two errors: "'self' used before all stored properties are initialized" and "Constant 'self.capitalCity' used before initialized"
class Country
{
let name: String
let capitalCity: City!
init(name: String, capitalName: String)
{
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
}
class City
{
let name: String
unowned let country: Country
init(name: String, country: Country)
{
self.name = name
self.country = country
}
}
reading an answer to a similar question I see that I can change let capitalCity: City! to var capitalCity: City! and the syntax error is resolved.
I realize that in this contrived example a country's capital city can change, so that would be fine, but what if there were a case where the value really was a constant...
Is there any way to resolve the syntax error while keeping capitalCity a constant?
In this case I would suggest you to make the property a variable but hiding it (make it seem like a constant) through a computed property:
class Country {
let name: String
private var _capitalCity: City!
var capitalCity: City {
return _capitalCity
}
init(name: String, capitalName: String) {
self.name = name
self._capitalCity = City(name: capitalName, country: self)
}
}
Is there any way to resolve the syntax error while keeping capitalCity a constant?
Not the way you have things set up. The source of the problem is actually that in order to set capitalCity, you have to create a City whose country is self. That is the use of self to which the compiler is objecting:
self.capitalCity = City(name: capitalName, country: self)
^^^^
Since you have configured City's country as a constant, you must supply this value when you initialize your City. Thus you have no way out; you must make capitalCity an Optional var so that it has some other initial value that is legal, namely nil. Your proposed solution actually works like this:
class Country
{
let name: String
var capitalCity: City! = nil // implicit or explicit
init(name: String, capitalName: String)
{
self.name = name
// end of initialization!
// name is set (to name), and capitalCity is set (to nil)...
// ... and so the compiler is satisfied;
// now, we _change_ capitalCity from nil to an actual City,
// and in doing that, we _are_ allowed to mention `self`
self.capitalCity = City(name: capitalName, country: self)
}
}
Just do:
private(set) var capitalCity: City!
which gives you the read-only public interface you want.
I understand that you find var capitalCity: City! contrived. I'd say that the selected answer is truly contrived; it adds lines of code that have no purpose other than resolving a language-related issue. Lines like that are meaningless and meaning is what matters most in code.
Came across this recently while struggling with a similar problem and as of swift 5.5 (or possibly lower) there is another alternative that is interesting. If you convert Capital City to a lazy var you are actually able to use self in initialization.
class Country
{
let name: String
lazy var capitalCity: City = {
City(name: capitalName, country: self)
}()
private let capitalName: String
init(name: String, capitalName: String)
{
self.name = name
self.capitalName = capitalName
}
}
class City
{
let name: String
unowned let country: Country
init(name: String, country: Country)
{
self.name = name
self.country = country
}
}
Cheers!
Related
I have the following code, which compiles & works fine:
import RealmSwift
struct Bucket: Codable, Identifiable {
var id: UUID
var title: String
init() {
self.id = UUID()
self.title = "new bucket"
}
init(title: String) {
self.id = UUID()
self.title = title
}
init(id: UUID, title: String) {
self.id = id
self.title = title
}
}
class RealmBucket : Object {
#Persisted var id : UUID
#Persisted var title : String
convenience init(_ id: UUID, _ title: String) {
self.init()
self.id = id
self.title = title
}
}
func loadBuckets() -> [Bucket] {
let realm = try! Realm()
let realmBuckets = realm.objects(RealmBucket.self)
return realmBuckets.map { Bucket(id: $0.id, title: $0.title) }
}
but if I change the loadBuckets() function to:
func loadBuckets() -> [Bucket] {
let realm = try! Realm()
let realmBuckets = realm.objects(RealmBucket.self)
let result = realmBuckets.map { Bucket(id: $0.id, title: $0.title) }
return result
}
(just the last line changed)
I get the error:
Cannot convert return expression of type 'LazyMapSequence<Results<RealmBucket>, Bucket>' to return type '[Bucket]'
If I change the let line to be:
let result : [Bucket] = realmBuckets.map { Bucket(id: $0.id, title: $0.title) }
then it works again.
I can think of a couple possible explanations for the behavior, but they all seem to point to a compiler bug, or language deficiency, and I'm guessing that perhaps there is some language feature I'm unaware of.
Does anyone know why the compiler is able to automatically convert the LazyMapSequence in the case of the return value being a variable, when it clearly knows the type of the variable given the error it is giving me. I'm relatively new to Swift, and hoping to learn something from this.
Based on the current answers, my assumption is that it is just a slightly different case in the compiler code to convert the variable versus a method call, so it's really just a compiler deficiency, and likely to not exist down the road. In any case, it's easy enough to work around, it just struck me as odd.
You have to explicitly define the return type of map function, when you use shorthand closure syntax, probably that is the reason.
A “return” statement knows the type it has to return, so if applies that knowledge when the right hand side is evaluated, and can often convert that result to the correct type.
A let statement with no type means the compiler doesn’t know which type is needed. So the right hand side can be evaluated differently.
I was testing out inheritance in Swift 3 and ran into an unexpected error. No matter how I alter the super.init() call, I can't make the error trace less problematic than this.
I have read through the documentation and other similar posts here on SO, but they don't have any examples of this particular problem. I am trying to create a class with properties that are either variable or constant and then creating another class that inherits from the first class and adds new properties.
The error comes from my subclass's init function: I set the new properties first, then call super.init() with the superclass's appropriate arguments. Swift then tells me that the third parameter "birthday" is an "extra argument."
Is there some sort of problem between my superclass having three properties and my subclass having two? I can't think of any other problem that could throw an error like this. Not sure which part is confusing me.
Here's my code:
class Person {
var name: String
var age: Int
let birthday: String
init(name: String, age: Int, birthday: String) {
self.name = name
self.age = age
self.birthday = birthday
}
}
class Student: Person {
var isEnrolled: Bool
var numberOfClasses: Int
init(isEnrolled: Bool, numberOfClasses: Int) {
self.isEnrolled = isEnrolled
self.numberOfClasses = numberOfClasses
super.init(name: name, age: age, birthday: birthday) {
self.name = name
self.age = age
self.birthday = birthday
}
}
}
Your initializer does not have a closure, so naturally, your approach cannot work. It says birthday is an extra argument, because the (non-existent) closure is the fourth argument in the list.
Student's initializer does not know what name, age or birthday is. You should include those parameters in the initializer as well and call super.init at the end.
init(name: String, age: Int, birthday: String, isEnrolled: Bool, numberOfClasses: Int) {
self.isEnrolled = isEnrolled
self.numberOfClasses = numberOfClasses
super.init(name: name, age: age, birthday: birthday)
}
By Drewag in his answer to the question
Every member constant must have a value by the time initialization is complete. Sometimes, a constant cannot be initialized with its correct value during initialization, but it can still be guaranteed to have a value before being accessed.
Using an Optional variable gets around this issue because an Optional is automatically initialized with nil and the value it will eventually contain will still be immutable. However, it can be a pain to be constantly unwrapping a variable that you know for sure is not nil. Implicitly Unwrapped Optionals achieve the same benefits as an Optional with the added benefit that one does not have to explicitly unwrap it everywhere.
The following code defines two classes, Country and City, each of which stores an instance of the other class as a property. Every country must have a capital city and every city must always belong to a country.
class Country {
let name: String
var capitalCity: City! //why I can't use let!
init(name: String, capitalName: String){
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
deinit {
print("\(name) has been de-initialized the city \(capitalCity.name) is gone with with the country")
}
}
class City{
let name: String
unowned let country: Country
init(name: String, country: Country){
self.name = name
self.country = country
}
deinit {
print("The city \(name) has been de-initialized ")
}
}
var country = Country(name: "Canada", capitalName: "Ottawa")
}
However, if I changed the line var capitalCity: City! into let capitalCity: City!, the compiler given out the following error warning.
Question: Isn't that we can use Implicitly Unwrapped Optional when a constant that cannot be defined during initialization? What's the error here?
The important part is here:
City(name: capitalName, country: self)
You are definitely using self in the expression which is executed before the assignment to the property capitalCity.
If you want to use self in any of the expressions, it needs to be in the second phase of two phase initialization, which means all properties needs to be initialized before the usage of self.
With using var, Swift assigns a default initial value nil for the property capitalCity. So the property can be considered as "already initialized", so, you can use self after you have initialized another property name.
(You know giving nil to a let-constant of ImplicitlyUnwrappedOptional is ridiculous.)
By the way private(set) var is often used in similar cases:
private(set) var capitalCity: City!
I've created the following class
class Person {
var firstName: String
var lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
Then I instantiated a constant value from the class
let john = Person(firstName: "Johnny", lastName: "Applessed")
Question: Why I can change the content of the variable john? Isn't it a constant? Can someone explain that for me, thanks a lot.
john.firstName = "John"
print(john.firstName) // -> John
As #Wain has said – it's due to the nature of reference types. The instance being a let constant only means you cannot assign a new reference to it – but says nothing about the actual mutability of the instance itself.
If you change your class to a struct, you'll see how the behaviour differs with value types, as changing a property changes the actual value of your Person – therefore you are unable to do so if it's a let constant. However I somewhat doubt you'll want to make your Person a struct, as two people with the same name shouldn't be considered to be the same person.
If you only wish your properties to be assigned upon initialisation (and then read-only for the lifetime of the instance), then I would recommend making them let constants (instead of making their setters private). This will ensure that you cannot even change their value from within your class, once assigned.
The rule is as long you give a property a value before the super.init() call – you can make it a let constant (in this case, you just have to assign them in the initialiser before using self).
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
...
The class instance itself is a constant, so you can't change it to reference another instance, but the instance is mutable because it's properties are created as vars.
Change firstName to have a private setter and see what you can do:
private(set) var firstName: String
When you're using a constant instance of a class in swift, doesn't mean you can't change the class attributes. It' means you can't instantiate a new object in this constant
let person = Person(firstName: "Johnny", lastName: "Appleseed")
person = Person(firstName: "John", lastName: "Appleseed") //--->It gets error: Cannor assign to value: 'person' is a 'let' constant
But you can create a constant inside class and set this values in the init
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
//Tip: Don't init the class constants in declaration time or will get the same above error. Just init this constants at constructor/initialization of class.
And Now you have the expected result you want, even if create a 'var' instance of this object
var person = Person(firstName: "Johnny", lastName: "Appleseed")
person.firstName = "John" //--->It gets error: Cannor assign to value: 'person' is a 'let' constant
person = Person(firstName: "John", lastName: "Snow")
person.firstName = "Johnny" //--->It gets error: Cannor assign to value: 'person' is a 'let' constant
Your thinking was not wrong, but a little confuse cause you would be totally right if it was a struct instead a class.
I'm looking at the example from the “Unowned References and Implicitly Unwrapped Optional Properties” section of the book “The Swift Programming Language.”
Their example code is
class Country {
let name: String
let capitalCity: City!
init(name: String, capitalName: String) {
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
}
class City {
let name: String
unowned let country: Country
init(name: String, country: Country) {
self.name = name
self.country = country
}
}
This works if I want to deal exclusively with Countries and the only purpose of the City type is to be a capital of a Country. But what happens if I want to create a City?
This creates a runtime exception because no reference to the City's Country is retained since it is an unowned variable:
var chicago = City(name:"Chicago", country: Country(name: "USA", capitalName: "Washington DC"))
chicago.country.name // Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
How would I allow something like this without creating a Strong Reference Cycle?
There are two typical solutions:
If you want to primarily deal with cities, invert the relationship so that City has a strong reference to Country, and Country points back to an unowned instance.
If you want to have cities and countries as primary objects that cross reference each other, put all cities and countries into collections (or other form of store that owns them), and make both references weak. That way they don't own each other, and you don't have a cycle.
The best way to avoid retain cycles is to consider who owns every object. Objects can own each other, but that should be a clear hierarchy (i.e. a tree). If you have connections that go sidewards and and upwards in the hierarchy, make them weak or unowned.
Solution one is the upwards case, solution two is sidewards case.
Edit
A third option is, to have Country own a collection of all its cities. I think that makes most sense in this simple case, but it means the Country needs to create all cities in it's initialization, or have a method that adds cities.
Here's an example for the second case. It's quite complex, probably too much so for this simple case, but it illustrates extracting a common owner. I would normally use it if there are a lot of cross references. (Think relational database. The records don't own each other.)
class Country {
let name: String
weak var capitalCity: City?
init(name: String) {
self.name = name
}
}
class City {
let name: String
unowned let country: Country
init(name: String, country: Country, isCapital: Bool) {
self.name = name
self.country = country
if isCapital {
country.capitalCity = self
}
}
}
class Planet {
var countries: [Country] = []
var cities: [City] = []
}
let earth = Planet()
earth.countries = [
Country(name: "USA"),
Country(name: "Canada"),
]
earth.cities = [
City(name: "Washington DC", country: earth.countries[0], isCapital: true),
City(name: "Chicago", country: earth.countries[0], isCapital: false),
City(name: "Ottawa", country: earth.countries[1], isCapital: true),
]
In your example, nobody is owning the Country instance. That means it gets deallocated (freed) immediately.
var country = Country(name: "USA", capitalName: "Washington DC")
var chicago = City(name:"Chicago", country: country)
chicago.country.name
will fix it because our coutry variable will keep USA from deallocating
If you use an unowned reference, you always have to keep a strong reference somewhere else.