1

The Problem:
I have a semi-large [Int] array, which contains ~ 25K to 60K elements. It was originally outputted into a text file by my first program and needs to be fed as a let value into a second program. To do this, I manually copied it in. However, Swift just freezes whenever I try to initialize it. By freeze, I mean that it doesn't do anything, even after an hour.

Further investigation:
I confirmed that it was the size of the array causing the freeze-up, by creating a test program with just one line that just said let test = [the_array]. That program is still running after an hour.

I have previously used arrays that contained 400-450K elements without any problems. However, those arrays didn't have to be initialized as a variable/constant, and each element only contained a number between 1-9. The array that is causing the freeze-up definitely has less than 100K elements, but each element contains an integer between 100-300K.

How am I able to initialize the array into the second program, and what exactly is causing the problem?

Other info: I'm using Swift 3.1.1 on Ubuntu 16.04 64bit, so I don't have access to Xcode. If you need the text file of the array, please leave a comment. File is uploaded here

Community
  • 1
  • 1
Imagen
  • 13
  • 3

1 Answers1

0

I downloaded your file - a 600KB file is a very small size for any modern computer. This codes ran in under 1 second on my 2012 iMac:

let fileURL = URL(fileURLWithPath: "/path/to/file.txt")

let charset = CharacterSet.whitespacesAndNewlines.union(CharacterSet(charactersIn: "[]"))
let fileContent = try! String(contentsOf: fileURL).trimmingCharacters(in: charset)

let array = fileContent.components(separatedBy: ",").flatMap {
    Int($0.trimmingCharacters(in: .whitespaces))
}

print(array.count) // 74061
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Your code worked seamlessly. Linux users have to remove the first line and instead use `let fileContent` load directly from `String(contentsOfFile: "file_location")`, as URL does not seem to be implemented in Foundations for Linux. There seem to be 1 issue though. When [1, 2, 3, 4, 5, 6, 7, 8] is passed in, the code after `print(array) gives a value of [2, 3, 4, 5, 6, 7]. And the count is always 2 less. – Imagen Jun 17 '17 at 14:37
  • For some reason, `.union(CharacterSet(charactersIn: "[]"))` did not seem to have any effect. If it worked for mac, it must be a Linux library problem. `let charset = CharacterSet.decimalDigits.inverted` fixed the problem. – Imagen Jun 17 '17 at 15:26
  • I'm not near a computer right now but that's an interesting observation. You should report the bug to Apple – Code Different Jun 17 '17 at 15:28