-1

I am bit confused here about Enums and initializing them.

here is the code:

enum WaveType {
    case close
    case far
}

class WaveNode: SKNode {
    ...
}

func addWave() -> WaveNode {
    let wave = WaveNode()
    ...
    return wave
}


var waves = [WaveType: WaveNode]()
waves[.far] = addWave()

var durations = [WaveType: TimeInterval]()
durations[.far] = 4

what is 'waves' in here? is it an array/dictionary of 'WaveType' Enum? if so, would you please explain waves[.far] = addWave() and durations[.far] = 4 ? Why do I think it should be something like waves = [.far : addWave()] or durations = [.far : 4]

Im sorry if it is too obvious, Thank you.

  • 1
    `waves[.far] = ....` assigns a value for key `WaveType.far` of an existing dictionary. In your case, the dictionary is empty before this assignment. Same for `duration[.far] = 4`. `waves = [.far : addWave()]` initializes the dictionary with a single key-value pair. The end result is the same – New Dev Jan 20 '21 at 07:12
  • Thank you @NewDev makes sense now. just to be clear, if we want to have `waves` dictionary with 2 key-value pair, how are we gonna do it? is this correct `waves = [.close : addWave() , .far : addWave()]` ? –  Jan 20 '21 at 07:43
  • 1
    Yes, that's correct. – New Dev Jan 20 '21 at 07:50
  • 1
    Right now, since you are using a dictionary, what you've built can only handle 2 waves: a close wave, and a far wave. I'm thinking that you might want more than 2 waves? Maybe 5 close waves? You may not, but if you do, you can try: `var waves = [(waveType: WaveType, waveNode: WaveNode, duration: TimeInterval)]()` this is an array (or list) of all existing waves, you won't be limited to only using 2. I've used a tuple so that you won't need multiple values like you did with `var waves` and `var durations`. (You will *not* like KeyValuePairs. Arrays are much better.) Let me know what you think. – 0-1 Jan 21 '21 at 03:05
  • 1
    Thank you so much @0-1 , This way I can make more waves later. wondering how can I make `addWave` function shorter ? As it is `func addWave(at position: CGPoint, zPosition: CGFloat, xScale: CGFloat, direction: WaveDirection) -> WaveNode`. Its not quite readable to use it like that in your tuple. Appreciate it –  Jan 21 '21 at 06:37

1 Answers1

0

What we discussed in the comments:

Personally, I would change the dictionary to an array. As I said before:

Right now, since you are using a dictionary, what you've built can only handle 2 waves: a close wave, and a far wave. I'm thinking that you might want more than 2 waves? Maybe 5 close waves?

Here is what your code will look like after migrating from a Dictionary to an Array:

enum WaveType { case close, far }
class WaveNode: SKNode {}
func addWave() -> WaveNode {}

// I have taken the initiative to group these up into a tuple:
var waves = [(waveType: WaveType, waveNode: WaveNode, duration: TimeInterval)]()

// Here's what adding a wave to the list would look like:
waves.append((waveType: .far, waveNode: addWave(...), duration: 4))

// Here's an alternate but identical way to add a wave to your array:
waves += [(
    waveType: .far,
    waveNode: addWave(...),
    duration: 4
)]

Still, this feels a little clunky as you say:

wondering how can I make addWave function shorter ? As it [has a lot of parameters]

Specifically you state these parameters:

  • position: CGPoint
  • zPosition: CGFloat
  • xScale: CGFloat
  • direction: WaveDirection

I think it can be made shorter, and I propose this can be done in 3 steps.


Step 1

Let's edit your WaveNode class to contain 2 new attributes: waveType and duration. Just as position, zPosition, xScale, and direction are all attributes of WaveNode.

class WaveNode: SKNode {
    // include these, as well as some default value
    var duration: TimeInterval = 0
    var waveType = .far
}

Step 2

Next, we will edit your addWave() method. I have edited it in 2 ways:

  1. I added underscores _ preceding each parameter tag to decrease length when calling the method.
  2. I added waveType and duration as parameters to your method as seen below:
func addWave(_ position: CGPoint,_ zPosition: CGFloat,_ xScale: CGFloat,_ direction: WaveDirection,_ waveType: WaveType,_ duration: TimeInterval) -> WaveNode {
    let wave = WaveNode()

    // Keep all code that was in between
    // Just add these 2 lines above the return line

    wave.waveType = waveType
    wave.duration = duration

    return wave
}

Step 3

Adding waveType and duration as attributes to WaveNode lets us not have to use an array of a tuple anymore. Instead, it will look like this:

var waves = [WaveNode]()

// And adding waves will be easy and much shorter:
waves.append(waveNode(...))

If you would like even shorter ways to rewrite this, I would be happy to oblige. Let me know what you think.

0-1
  • 702
  • 1
  • 10
  • 30
  • Thank you @0-1,I appreciate the thorough explanation. its much cleaner now :) Cheers –  Jan 22 '21 at 08:16