Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Beginning Core Graphics Part 4: Contexts 2

Learn about the older style of Core Graphics drawing and also how to replace the icon images with the icon paths.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3402-beginning-core-graphics/lessons/5

This continues to be a great series. Thank you!

I wonder if there is a stylistic decision by the RW team, or just yourself, to use
lazy var iconPath:UIBezierPath? = drawIcon(self)()
instead of
lazy var iconPath:UIBezierPath? = self.drawIcon()

Why self is necessary, is a thing that confused me, some months back. I learned that there really aren’t such things as instance methods in Swift, but rather, only curried static methods that take an instance as the first parameter. I presented at CocoaHeads about it, but outside of lazy variables, I haven’t really seen it come up much, in code, and nobody really knew what I was talking about due to esotericism. :smile_cat: Personally, I still don’t think that self should be required, but I’ve been using the latter form, while you formed your forms with the former form.

1 Like

As you know, I lovingly handcraft each line of code, but I suspect here I may have been thinking about drawing the icon rather than about Swift language, although it is possible that I had a conversation with myself about this several months ago that I don’t remember.

There’s no stylistic decision on this, and today I too prefer self.drawIcon().

I’m not a Swift language buff, but I think self is necessary, because it’s actually a closure. After playing around with it a bit, this is what initialisation would be if I weren’t using a method:

lazy var iconPath:UIBezierPath? = {
    [unowned self] in
    var path:UIBezierPath?
    switch self.name {
    case "Fun":
      path = DrawIcon.drawFunIcon()
    default:()
    }
    return path 
}()
1 Like

Great Tut! drawing a blank though, how are you able to access the categories data from the MainViewController? I don’t see any references and it doesn’t appear to be in the model class. I feel like I’m missing something very small.

Thanks!

@jgoldsmith - categories is defined in Category.swift as a global variable.

This isn’t good practice, but I do it in tutorials so that I don’t have to spend time in each tutorial explaining things that aren’t relevant.

Ah! Having it outside of the class definition makes it global. Of course! Thanks for responding so quickly!