I'm trying to write some code for seeding some test data into the Core Data database in an application I'm developing, about Pokémon. My code for seeding is based on this: http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/
I am having a slight problem with one thing though. I don't seem to be able to put a nil value inside of a tuple.
I'm currently trying to seed some Pokémon Moves into the database. A move can have a bunch of different properties, but which combination it has it entirely dependent on the move itself. All seed Move data is in an array of tuples.
To demonstrate...
let moves = [
(name: "Absorb", moveType: grass!, category: "Special", power: 20, accuracy: 100, powerpoints: 25, effect: "User recovers half the HP inflicted on opponent", speedPriority: 0),
// Snip
]
...is fine. It's a move with all the above properties, where zero in speedPriority means something. However, some moves don't have a power or accuracy property, because they're irrelevant to that specific move. However, creating a second tuple in the array without the power or accuracy named elements, such as...
(name: "Acupressure", moveType: normal!, category: "Status", powerpoints: 30, effect: "Sharply raises a random stat", speedPriority: 0)
...understandably throws an error
Tuple types {firstTuple} and {secondTuple} have a different number of elements (8 vs. 6)
because, well, the tuples have different number of elements. So instead, I tried...
(name: "Acupressure", moveType: normal!, category: "Status", power: nil, accuracy: nil, powerpoints: 30, effect: "Sharply raises a random stat", speedPriority: 0)
but this also didn't work, as it gave the error:
Type 'Int' does not conform to protocol 'NilLiteralConvertible'
So, is there any way to do what I'm trying to do? Is there some way to either place a nil value inside the tuple, or somehow make it an optional element? With thanks!
You could do something like below:
Add more parameters as you wish, I took just two parameters for explanation of concept.
In the tutorial the tuples only contained two values -
name
andlocation
. Since you have so many variables in one tuple you should conside putting them into a class or struct.Using Structs or Classes also makes it easy for variables to be nil. And, since optional types have a default value of
nil
when setting up each move you only need to set the values that aren't nil - as shown the second example.An example Struct is:
You could then create a move:
In the tutorial you need to enumerate your array of Tuples, for this to work with Structs it basically looks the same:
You can achieve that using: