0

In the code below I'm confused as to what "self" actually refers to.

let imageURL = URL(string : “http://exampleURLh.jpg”)!
let task=imageDataURLSession.sharedSession().dataTaskWithURL(imageURL, {(data, response, error) in
    print (“task finished”)
}
task.resume()
if error == nil {
    let downloadedImage = UIImage(data: data!)

    self.imageView.image = downloadedImage
}

I know that I'm setting downloadedImage as the imageView using its image property, but I'm not sure why self is needed and what piece of data it holds in this situation. I know it refers to an instance of the viewController class being worked on, but is that instance the UIImage, downloadedImage, or the resource data from the image at the example URL?

  • What class is this code contained in? – Carcigenicate Aug 26 '17 at 23:26
  • 3
    It refers to whatever class this code is in. – rmaddy Aug 26 '17 at 23:26
  • 1
    Probably a duplicate of https://stackoverflow.com/questions/26835013/what-is-self-used-for-in-swift – rmaddy Aug 26 '17 at 23:27
  • its within the .viewDidLoad function which is inside the view controller. – Christian Beecher Aug 26 '17 at 23:30
  • Then `self` is the view controller instance. – rmaddy Aug 26 '17 at 23:34
  • Yes i know this, I should've worded the question better. What is the actual instance in this case? What does it specifically refer to? Ive reworded my question. – Christian Beecher Aug 26 '17 at 23:37
  • 2
    Your question makes little sense. `self` is the instance of the view controller just as you state. That's it, nothing else. `self.imageView` is referring to the `imageView` property of the view controller. – rmaddy Aug 26 '17 at 23:42
  • Im thinking with a javascript like mindset where you specify the class and then an object name and then you use that object to access a property to do some task. And the object is clearly named and stated. I was hoping that self referred to something that was clearly created as well. But to my understanding youre saying that self is the instantiation of an object itself. And to refer to this object I must use "self"? – Christian Beecher Aug 26 '17 at 23:50
  • Once you've figured _this_ out, you'll also need to deal with [the asynchronous nature of the task](https://stackoverflow.com/questions/22267865/returning-method-object-from-inside-block), because this code isn't doing what you think it is. – jscs Aug 26 '17 at 23:56
  • yeah i understand this will block on the main queue. Im just trying to completely understand what self is because i see it too often not to have an understanding of it. – Christian Beecher Aug 26 '17 at 23:59

2 Answers2

1

Because of class inheritance, it is impossible to know just by looking at code what self refers to. It all depends on what self is at runtime.

self means the actual instance to whom the message was originally sent, but you haven't even shown the method in which this code is wrapped, so there isn't enough information even to guess.

If you are in doubt of what self is on some particular occasion, log it (with NSLog or print).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Might help you to read my explanation here: http://www.apeth.com/swiftBook/ch01.html#_self And here: http://www.apeth.com/swiftBook/ch04.html#_polymorphism – matt Aug 26 '17 at 23:50
  • so "self" creates an instance of the class and is itself the instance of the class? Thats what i got from your link – Christian Beecher Aug 26 '17 at 23:53
  • 2
    No. Self does not **create** any instances. Self just refers to the instance of the class it is in the scope of. If there is no instance of that class then the code that contains `self` will not be run because there is no instance. Think of `self` a bit like a property of every class that refers to itself. Just like imageView is a property of the view controller in this case. – Fogmeister Aug 27 '17 at 00:10
  • 3
    When a person says “**my** name is Fogmeister” there is not another instance of a person cloned. The word “my” refers to the instance of the person that said that word. If that makes sense. – Fogmeister Aug 27 '17 at 00:12
0

When you call an object's (non-static/class) function, you call it like this:

obj.function()

Inside of function(), self refers to the object it was called on (so obj in this case). self is like an automatic variable you get made for you to make it convenient to refer to the target of the function call.

In your case, it's pretty clear that this code is from a ViewController of some sort. That VC has an imageView property that represents (probably) a UIImageView in the VC. I'm guessing your code is inside a function that is either called by viewDidLoad (or similar) or perhaps it's the target of a button tap.

When those functions are called, self will refer to the ViewController object that the function was called on.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Very good, but I would suggest adding "Inside of function() at any depth". My point is that if function() calls `self.function2()`, `self` inside function2() is still `obj`. And all of this is true _even if function or function2 is inherited_. That's why I referred the OP to a discussion of polymorphism... – matt Aug 27 '17 at 00:16