Referring to self in property initializer
Referring to self in property initializer
- Subject: Referring to self in property initializer
- From: Rick Mann <email@hidden>
- Date: Fri, 14 Aug 2015 15:44:34 -0700
I'm having a hard time with this code:
------------------------
import Foundation
class MyDelegate : NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate
{
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL)
{
}
var
backgroundSession: NSURLSession =
{
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("myident")
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
return session
}()
}
playground18.swift:14:23: error: cannot invoke initializer for type 'NSURLSession' with an argument list of type '(configuration: NSURLSessionConfiguration, delegate: MyDelegate -> () -> MyDelegate, delegateQueue: nil)'
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
^
playground18.swift:14:23: note: overloads for 'NSURLSession' exist with these partially matching parameter lists: (configuration: NSURLSessionConfiguration), ()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
^
------------------------
What is the type of "self" in the initializer closure, and is there a way to refer to the enclosing class instance? I also had trouble locating this in the Swift 2 guide, if it's there at all.
I can do it with a separate member function and call that in the initializer, but that introduces a lot of boilerplate, as well as creating a method I don't intend to ever be called separately.
--
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