0

When I create a recursion in a didSet actually I turned out i could just put a return and program exits from didSet . But i did not find anywhere (i was searching for a long time) that i can put a return word to exit from didSet. So, is didSet works like a computed property where we can return value? Please if someone know anything i would very appreciated. Thank you.

 class AppDetailController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var app : App? {
    didSet {
        if app?.screenshots != nil {
            return 
        }
        if let id = app?.id {
            let urlString = "https://api.letsbuildthatapp.com/appstore/appdetail?id=\(id)"
            URLSession.shared.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, error) in
                if error != nil {
                    print(error)
                }
                do {
                let json = try(JSONSerialization.jsonObject(with: data!, options: .mutableContainers ))
                    let appDetail = App()
                    appDetail.setValuesForKeys(json as! [String: AnyObject])


                   self.app = appDetail
                } catch let error as NSError {
                    print(error.localizedDescription)
                }
            }).resume()
        }

     }
}
Ninja
  • 309
  • 7
  • 26
  • 1
    A `return` exits the scope, doesn't return a value... Think of it (it is actually) a closure that is called every time the value is set (after initialization). – Alladinian Apr 25 '18 at 13:00
  • @Alladinian but actually there is no mentions about this fact. + if i can return from didSet scope can I return from another scope? maybe return from if scope? just a plain if scope, can i? – Ninja Apr 25 '18 at 13:05
  • 1
    [Here you go](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html). And regarding closure vs function, [see this one](https://stackoverflow.com/questions/24108667/what-is-the-difference-between-functions-and-closures) – Alladinian Apr 25 '18 at 13:15
  • @Alladinian so didSet is a closure or method returning void? Where did find this? I mean I just did not find any single word about it – Ninja Apr 25 '18 at 13:19
  • 1
    It is the same... It cannot be anything else. – Alladinian Apr 25 '18 at 13:21
  • @Alladinian why it is not like an if?by the way it has the same meaning. Does the meaning of that words "It is the same... It cannot be anything else" mentioned in any official docs? – Ninja Apr 25 '18 at 13:26
  • 1
    Ok if you _really_ want to get to the bottom of this you can see in the [swift grammar](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/zzSummaryOfTheGrammar.html) that `didSet` expects a `code-block` just like `if`, `else` or `guard`... and we _do_ know that we are supposed (expected in fact) to `return` from a guard right? – Alladinian Apr 25 '18 at 13:32
  • 1
    and digging deeper you can see that `return` is a `statement` and `code-block` can include `statements`, so here is your official confirmation. – Alladinian Apr 25 '18 at 13:43
  • Well. 1) we can use control transfer statements inside the function body, and the fact that we can return from guard or if statement is just because they cannot be place nowhere besides function body. 2) that fact that didSet expect code-block and if or guard statements expects code block also and if we can use return statement in if and guard body do not implies that we can use it didSet, because that fact that allow use return statement in if and guard body is that they are place inside the function body. – Ninja Apr 25 '18 at 14:04
  • So allowance of using return in didSet body is that it is a function. But i did not find the place where it says that didSet is a function, or it has a function body – Ninja Apr 25 '18 at 14:04
  • 1
    Again, the grammar is clear. You can use `statement`s (and by extension a `return` - which is a statement) in **_every_** `code-block`. Isn't that enough to answer "why I can use `return` in `didSet`"? – Alladinian Apr 25 '18 at 14:12
  • it turns out enough, and this fact that I can use statements in every code-block implies that didSet has a function body. Thank you very much – Ninja Apr 25 '18 at 14:14
  • Sure. That was a fun exercise btw – Alladinian Apr 25 '18 at 14:16

3 Answers3

1

No, didSet does not have any relation to computed property. Actually didSet {} and willSet are for stored properties, and they are play the role of didChangeValue and willChangeValue. So you can change other data in your class or struct, or run some method in according to changing the value of the property that you want to define didSet and willSet for it.

They're working like a function and have a property. newValue for willSet that gives you new value will set to your property and oldValue for didSet that gives you the old value for your property before changing its value. You can change the name of these properties like this:

willSet(myNewValue) { }
didSet(myOldValue) { }
  • but why can i return from didSet? is it a closure or function? And if it is where did find info about it? I was looking for it and did not found it( – Ninja Apr 25 '18 at 13:10
  • You can find more information, about stored properties, computed properties and of course didSet and willSet from this link that is belong to apple swift learning: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html – Seyed Samad Gholamzadeh Apr 25 '18 at 13:15
  • Accept my answer, if you liked it. – Seyed Samad Gholamzadeh Apr 25 '18 at 13:16
  • Sorry but you did not answered a question i hoped that you could continue your answer – Ninja Apr 25 '18 at 13:27
0

didSet is a property observer, and it is called every time a new value is stored in a stored property.

Consider didSet like a method returning void. You check for a condition in your code, if met, you may return so that the rest of the code may not be executed.

SWAT
  • 1,107
  • 8
  • 19
  • But where did you find that you can consider didSet like a method returning void? Actually it is logical. But i feel unsettled because i can find official information about it( – Ninja Apr 25 '18 at 13:07
  • "Consider". I never said that it is. – SWAT Apr 25 '18 at 13:12
  • ok, well, just consider, but why it acts like method returning void? I just mean that i did not find any confirmation that i can return from didSet, so it seems it implied because of a type if didSet construction, but yet again, i could not find any affirmation about didSet is like a func and we can return from it. – Ninja Apr 25 '18 at 13:23
0

The thing in curly braces after the term willSet or didSet is a function body. The way you exit from a function body is by saying return.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • matt, and the thing in curly braces after the term " if " also a function body? – Ninja Apr 25 '18 at 13:34
  • No, but it might well be _in_ a function body. In fact, I think we can guarantee that. It is a code block. Not every code block is itself a function body, but it must occur in a function body. — Forgive me for suspecting that you know all this and are just being disingenuous. – matt Apr 25 '18 at 13:36
  • Well I had contemplated this question and find out that we always can return from if, because if is always in function scope otherwise i got an compile time error. So if i can return from didSet, it is certainly function body, because (iа i am not wrong) otherwise we could got an error. But main problem for me and i questioned it : why this fact that this is function body did not mentioned anywhere? How should i understand that it is a function? they just named it property observer and thats all. so of course i am not clear about why can i treat it like a function. Thank you – Ninja Apr 25 '18 at 13:46