I am trying to use MultipeerConnectivity framework with Swift. I have following properties:
var peerId: MCPeerID?;
let advertiser: MCNearbyServiceAdvertiser;
let browser: MCNearbyServiceBrowser;
var session: MCSession?
....
In my init method, I initialize all my stored properties like this:
init() {
let defaults = NSUserDefaults.standardUserDefaults();
let dataToShow = defaults.dataForKey("kPeerID");
peerId = NSKeyedUnarchiver.unarchiveObjectWithData(dataToShow) as? MCPeerID;
if !peerId {
peerId = MCPeerID(displayName: UIDevice.currentDevice().name);
let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(peerId);
defaults.setObject(data, forKey: "kPeerID");
defaults.synchronize();
}
advertiser = MCNearbyServiceAdvertiser(peer: peerId, discoveryInfo:nil, serviceType: "stc-classroom");
browser = MCNearbyServiceBrowser(peer: peerId, serviceType: "stc-classroom");
session = MCSession(peer: self.peerId);
super.init();
session!.delegate = self;
advertiser.delegate = self;
browser.delegate = self;
}
Now as you can see there is quite a bit lines of code to assign value to peerId (Apple recommended way), so I thought it would be nice to put that logic in a computed property so I replaced peerId stored property with a computed property and init method like this:
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!;
}
Now my init method is much cleaner and shorter like this:
init() {
advertiser = MCNearbyServiceAdvertiser(peer: peerId, discoveryInfo:nil, serviceType: "stc-classroom");
browser = MCNearbyServiceBrowser(peer: peerId, serviceType: "stc-classroom");
session = MCSession(peer: self.peerId);
super.init();
session!.delegate = self;
advertiser.delegate = self;
browser.delegate = self;
}
But now compiler complains as I am trying to access self while accessing peerId in browser/advertiser/session initialization. I understand in Swift all the properties must be initialized before we can use them and self is called implicitly and hence the error. But apparently that means I can't call computed properties in the init method, so I want to know if there is any better way of implementing what I am trying to do or not? I understand that I can create stored properties for every computed property and treat it as an iVar but it doesn't seem like an elegant solution either.
I would really appreciate your help regarding this guys.