0

I have the following dictionary of arrays:

var Levels = [1:[0,3,1,1,3,2,0,9,5,4,3,1,0,9,8,2],2:[5,2,9,1,3,2,0,8,5,4,3,1,0,9,8,6],3:[5,2,7,1,3,2,0,4,5,4,3,3,0,9,8,3],4:[2,5,1,2,6,2,0,9,5,4,3,1,0,9,8,1]] //...and so on, up to ~900

Each array is different from the last. I have about 1000 of these generated, and I did consider using generation within the program, but also thought it may cause the user to experience lag waiting for a new level to load. It's a number puzzle, and pretty simple in terms of coding, but this large dictionary has been slowing down Xcode and causing the indexing to go nuts. I get this error now that I've never seen before when trying to build:

Command failed due to signal: Segmentation fault 11

Is there a better way to arrange about 1000 arrays like this? How should I set that up? I need to reference a key or something too, instead of using individual variables for each level. Thanks!! - GG

gg1789
  • 31
  • 6
  • Absolutely not, the moderator deleted more than half of my text thinking it was irrelevant. I'm not an idiot lol. That would be hilarious though right... Order doesn't matter, but my levels are generated... so needing a simple incremation, "1,2,3,4" is useful for automation when calling that key. Edit: I changed that up to avoid future confusion. Thanks! – gg1789 Dec 01 '15 at 16:44
  • "thought it may cause the user to experience lag waiting for a new level to load" That's silly. You only have to do this once, and you have no evidence that it will take more than a millisecond. – matt Dec 01 '15 at 16:55
  • the generation isn't super optimal yet, because the numbers have to work in a certain way, they're random but they have rules- I guess sudoku is a way you can think of it. I'm checking many things to generate those numbers because of how the game is structured. Takes at millisecond sometimes, but also 2-3 seconds other times... I can keep trying to optimize it though, there's definitely a way. – gg1789 Dec 01 '15 at 17:02
  • Two comments. First, the Swift compiler works much better when you tell it the type of the large literal: `var levels:[Int:[Int]] = [...`. Second, why use a dictionary when your keys are just level numbers? You could just use an array of arrays and index into it with `level - 1`. – vacawama Dec 01 '15 at 18:08
  • I did it your way the first time, but ran into problems when converting a string to an Array variable that can be adjusted incrementally and read by another variable. I must have typed dozens of lines to try and get that working, using every syntax trick I knew and It just wasn't working lol. So dictionary was the working solution for that, and the keys may have different values later. For easy/medium/hard I'll have something else to identify 1E/1M/1H for example. Although if I can just make a big dictionary this would be easy! I can separate easy medium and hard based on the number... – gg1789 Dec 01 '15 at 18:49

2 Answers2

3

The Swift compiler doesn't like huge literals. You will have to assemble this value in code. (In the early days, I was able to get the same issue just by writing a single expression concatenating a dozen literal strings. The solution was to write a dozen string variables and concatenate those.)

You can file a bug report, and probably should, but it will probably come back as a duplicate, since I'm fairly sure Apple knows about this.

matt
  • 515,959
  • 87
  • 875
  • 1,141
2

If you have 1000 arrays, why don't you put then in a property list file/files and read them in at runtime? Do you really need all of that level data in memory at all times?

if let 
path = NSBundle.mainBundle().pathForResource("Level1-10", ofType: "plist"), 
dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject],
level1 = dict["1"] as? Array<Int> {
    // Use level1 array here
}
Mr Beardsley
  • 3,743
  • 22
  • 28
  • This sounds like a great idea, but completely out of my knowledge (I self taught myself all this, so it's been a grind, and core data is my current challenge lol) but what would this "file" look like. A text file? I think this is the method to go for... – gg1789 Dec 01 '15 at 17:04
  • This link is a little older, but gives a great overview of how to get started. http://stackoverflow.com/questions/9044735/steps-to-create-and-edit-a-plist-file-in-xcode Also, the code above is how you would read data out of the plist at runtime. – Mr Beardsley Dec 01 '15 at 17:18
  • I see "Level1-10" is the name of the file right? of type "plist" I understand too. Thanks! I'll check out that link later when I get a bit more time to really sit down and read it. – gg1789 Dec 01 '15 at 17:20
  • Yeah, you might chunk up your levels into smaller, more manageable, sections. I just used "Level1-10" as an example. You could do whatever works best for your project. – Mr Beardsley Dec 01 '15 at 18:13
  • Thanks, I'll give it a shot later – gg1789 Dec 01 '15 at 18:51