Re: Referring to self in property initializer
Re: Referring to self in property initializer
- Subject: Re: Referring to self in property initializer
- From: Rick Mann <email@hidden>
- Date: Fri, 14 Aug 2015 18:47:29 -0700
So, adding lazy lets it work with the closure (makes self available to the closure):
lazy var backgroundSession : NSURLSession =
{
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.matterport.StandaloneShowcase.ModelFetchConfiguration")
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
return session
}()
Without lazy, you cannot initialize it with a call to a member (e.g. = self.createbackgroundSession()). You also cannot initialize it in init(), because of the restrictions on initializing and using self. This to me is one of the more cumbersome areas of the language.
I really wish you could do lazy let foo = .... I don't see why the language can't support that.
> On Aug 14, 2015, at 17:44 , Quincey Morris <email@hidden> wrote:
>
> On Aug 14, 2015, at 17:26 , Jens Alfke <email@hidden> wrote:
>>
>> (Either way, it would still make sense for ‘self’ to be available.)
>
> Yes and no. Because it’s an ivar, the ivar’s value is associated with an instance rather than a class, but:
>
> — It’s being initialized by an arbitrary closure. There’s no particular reason to regard the closure as associated with any specific instance.
>
> — It’s initializing an ivar. There’s no (accessible) ‘self’ object until all of the class’s own ivars are initialized. Similarly, in an initializer, you cannot assign ‘self’ to anything, until all ivars are initialized. So even if ‘self’ was taken to mean “the” instance, it would still be an error.
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
--
Rick Mann
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden