• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Referring to self in property initializer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Referring to self in property initializer
      • From: Quincey Morris <email@hidden>
    • Re: Referring to self in property initializer
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Recurrent NSInvocationOperations on an NSOperationQueue?
  • Next by Date: Re: Referring to self in property initializer
  • Previous by thread: Re: Recurrent NSInvocationOperations on an NSOperationQueue?
  • Next by thread: Re: Referring to self in property initializer
  • Index(es):
    • Date
    • Thread