0

In this code:

enum Dog {
    case Snoopy
    case Lassie
    case Scooby
}

let offset = 3   
let dogRange = [ Dog.Snoopy : ( offset + 1 ... 5 ), Dog.Lassie : ( 6 ... 10 ), Dog.Scooby : ( 11 ... 15 ) ]

I do not get an error in Swift. However, if I encapsulate in a class

enum Dog {
    case Snoopy
    case Lassie
    case Scooby
}

class animal {
    let offset = 3  
    let dogRange = [ Dog.Snoopy : ( offset + 1 ... 5 ), Dog.Lassie : ( 6 ... 10 ), Dog.Scooby : ( 11 ... 15 ) ]
}

I get the error Instance member "offset" cannot be used on type "animal". I've tried changing to self.offset, but I then I get the error Use of unresolved identifier "self". Why, and how can I use a computed range in a dictionary in a class?

Thanks in advance!

Dribbler
  • 3,923
  • 6
  • 30
  • 51
  • 2
    This was closed as a duplicate without letting the user know why it was a duplicate. The issue is that dogRange is dependent on offset... both of which are properties. In this particular case, you can make offset static: `static let offset = 3` – Good Doug Jan 21 '16 at 18:23
  • Thanks, @GoodDoug! I had looked but not found a solution... that really helped! – Dribbler Jan 21 '16 at 18:30
  • Well, I thought that the title of the duplicate (and the explanations in the answer) makes that obvious. – There are various solutions, moving the initialization into a constructor is another one: http://stackoverflow.com/questions/25582853/type-does-not-have-a-member – Martin R Jan 21 '16 at 18:31
  • I read both of what you pointed to, @MartinR, and it did not seem to me to be a duplicate. But then, I am just learning Swift, and defining a range in a dictionary does not seem exactly like either of those, whereas someone more familiar with the language and its inner workings such as yourself might see how it is a duplicate. I do appreciate Good Doug's very concise and understandable answer. – Dribbler Jan 21 '16 at 18:34
  • @Dribbler: The problem is that the initialization of the property `dogRange` (i.e. what comes to the right of `let dogRange = ...`) depends on `offset`, which is another property of the same class. That's why I closed it as a duplicate of *"How to initialize properties that depend on each other"* – it is the identical problem. Then I provided another link to an alternative solution of the same problem. – Martin R Jan 21 '16 at 18:39
  • 1
    I get it now, @MartinR, but if you look at both examples, what is a property is quite different. To someone not understanding that... me 10 minutes ago, for example... I was still scratching my head after reading and re-reading and re-reading what you pointed me to. And I can imagine that someone with a similar problem to me, traversing your links would not necessarily help either (but I could be wrong.) However, in one terse sentence, Good Doug explained the problem and the solution. That really helped, and I'm so glad that he took the time to do so after the Duplicate gate shut. – Dribbler Jan 21 '16 at 18:43

0 Answers0