Another convention to keep Swift concise and clear

I love all the ways Swift lets you keep code together when it belongs logically together. If Swift adds the convention that properties in a class that get initialized in the declaration could be available in the order they occur (or a better order if possibe), then we could have more complex declarations in one statement, rather than in 2 places, and avoid the possibility of unnecessary optional if the init is not the right place to initialise the properties.


For example, it would be very elegant if this code snippet were doable:


class Item {

var data: String

init(inout list: [Item], data: String) {

self.data = data

list.append(self)

}

}


class ItemList {

var items = [Item]()

var thing1 = Item(list: &items, data: "first")

var thing2 = Item(list: &items, data: "second")

}

A nice thought, but this shouldn't be here. This is just for iOS 9 dicussions. I'd suggest putting this under Swift, which you can find in the Xcode forum 🙂

How is this elegant? You're tying the Item object to the container that holds it, something that the object should never need to know. What if you later decide that Item objects can be stored in a dictionary instead of an array?

Sure. I was looking for a Swift or better yet a Swift 2 group.

The objects are conceptually tied to that object, so that is fine.

The elegance comes from having fewer places in the code that would need to be read. There is a list of objects that need to be declared, and the same list needs to be in an array. If the list changes, there is in the code I suggest one and only one place that needs to be changed.

Another convention to keep Swift concise and clear
 
 
Q