I'm having a problem getting my default_ip from my UserDefaults to a SocketManager.
final class Service: ObservableObject{
let defaults = UserDefaults.init(suiteName: "group.com.Scheduled-countdown.settings")
let manager = SocketManager(socketURL: URL(string: "http://172.20.10.4:3000")!, config: [.log(false),.compress,.path("/ws")])
init(){
let default_ip = defaults?.string(forKey: "ip_adress") ?? "Nothing"
let ipString : String = "http://\(default_ip):3000"
//let manager = SocketManager(socketURL: URL(string: ipString)!, config: [.log(false),.compress,.path("/ws")])
print(ipString)
print(default_ip)
}
}
if I have the SocketManager above init() it connects but I can't have the default_ip because above because it says
Cannot use instance member 'defaults' within property initializer; property initializers run before 'self' is available
So how would I fix this?
I also uploaded the video to show the problem https://www.youtube.com/watch?v=teqr20AnTVM
Changing to lazy var helped me.
final class Service: ObservableObject{
let defaults = UserDefaults.init(suiteName: "group.com.Scheduled-countdown.settings")
lazy var default_ip = defaults?.string(forKey: "ip_adress") ?? "Nothing"
lazy var ipString : String = "http://\(default_ip):3000"
lazy var manager = SocketManager(socketURL: URL(string: ipString)!, config: [.log(false),.compress,.path("/ws")])
init(){
print(ipString)
print(default_ip)
}
}