I have a lazy property in swift that has a callback that looks like this.
lazy var apiClient: MyApiClient =
{
var apiClient : MyApiClient = MyApiClient()
apiClient.detailSearchFinishedCallBack = {
(detailModel : DetailModel!) in
}
return apiClient
}()
I have another lazy load property that I would like to access inside the closure that looks like this:
lazy var loadingView : LoadingView =
{
var loadingView : LoadingView = LoadingView()
loadingView.frame = CGRectMake(0, 0, 200, 200)
loadingView.center = self.view.center
return loadingView
}()
However, I'm unable to reference the loading view inside the closure. In objective c, this would look something like this.
-(LoadingView *)loadingView
{
if(!_loadingView)
{
_loadingView = [LoadingView new];
_loadingView.frame = CGRectMake(0, 0, 200, 200);
_loadingView.center = self.view.center;
}
return _loadingView;
}
-(MyApiClient *)apiClient
{
if(!_apiClient)
{
_apiClient = [MyApiClient new];
__weak FeedViewController *_self = self;
self.apiClient.detailSearchFinishedCallBack = ^(DetailModel *detailModel)
{
[_self.loadingView stopAnimating];
};
}
return _apiClient;
}
Would someone be kind enough to show me the equivalent of this in Swift?
UPDATE:
lazy var apiClient: MyApiClient = {
let apiClient: MyApiClient = MyApiClient()
apiClient.detailSearchFinishedCallBack = { [weak self] (detailModel: DetailModel!) in
println(self?.loadingView.frame)
return
}
return apiClient
}()
So I went ahead and tried implementing the proposed solution but I get a compilation error. Specifically I'm running into this error:
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
I'm not sure if this is something to do with my swift configuration in my project or not but I'm using the bridging header to import my objective c header files to be exposed to Swift. I can't think of anything else that could be causing this but any help would be appreciated to resolve this.