Swift Computed Properties Cannot Be Used in Init

Swift Computed properties cannot be used in init?

The Swift book doesn't address initialization & computed properties directly, but it has relevant information in the section about inheritance and initialization:

Two-Phase Initialization
Class initialization in Swift is a two-phase process. In the first phase, each stored property is
assigned an initial value by the class that introduced it. Once the
initial state for every stored property has been determined, the
second phase begins, and each class is given the opportunity to
customize its stored properties further before the new instance is
considered ready for use.

Basically, you need to assign an initial value to all of your non-Optional stored properties (looks like advertiser and browser) before you can call any methods or access any computed properties. You might instead want to initialize peerId with a closure, using this method:

var peerId: MCPeerID = {
let defaults = NSUserDefaults.standardUserDefaults()
let dataToShow = defaults.dataForKey("kPeerID")
var peer = NSKeyedUnarchiver.unarchiveObjectWithData(dataToShow) as? MCPeerID
if peer == nil {
peer = MCPeerID(displayName: UIDevice.currentDevice().name)
let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(peer)
defaults.setObject(data, forKey: "kPeerID")
defaults.synchronize()
}
return peer!
}()

Swift: why lazy, computed property, and property observer can not be let

Lazy properties : You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

computed property : whereas computed properties calculate (rather than store) a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

property observer : property observers is to monitor changes in a property’s value, if you define it let then how you can monitor changes because let is one type of constant which you can not change after init.

How can I compute and initialize in init content property ? swift

Simply create properties for width and height in class Grid. In the init(width:height:) set values for these properties.

Create content as computed property that returns the result of setupForNewGame(width:height:) method using the above created width and height properties.

class Grid {
let width: Int
let height: Int

var content: [[Int]] { //this is a computed property.....
return Grid.setupForNewGame(width: self.width, height: self.height)
}

init(width: Int, height: Int) {
self.width = width
self.height = height
}

class func setupForNewGame(width: Int, height: Int) -> [[Int]] {
return ...
}
}

Usage:

let grid = Grid(width: 3, height: 3)
print(grid.content) //prints the result returned from `setupForNewGame(width:height:)` method using width = 3 and height = 3

StateObject as parameter for another object in init()

Use computed property

struct Test: App {

@StateObject var user: User = User()
var authenticationHelper: AuthenticationHelper {
return AuthenticationHelper(user: user)
}

init() {
}
}

You can also use like this

struct Test: App {

@StateObject var user: User
var authenticationHelper: AuthenticationHelper

init() {
let user = User()
self._user = StateObject(wrappedValue: user)
self.authenticationHelper = AuthenticationHelper(user: user)
}
}

Swift/SwiftUI property initializer error in struct

You are using name before the struct is initialized. If you make message a computed properties it should work.

struct Greeting {
var name = "Bob"

var message: String {
"Hi, " + name
}
}


Related Topics



Leave a reply



Submit