0

I am trying to fill an array by loading it from a text file and then separating the text with line breaks to formulate an array however i am getting an error "Expected Expression"

  var marrCountryList = [String]()
  try!  var text = String(contentsOfFile: "country", encoding: NSUTF8StringEncoding)
  marrCountryList = text.componentsSeparatedByString("\n")
melwyn pawar
  • 1,766
  • 2
  • 16
  • 21

1 Answers1

0

The try is not in the right position:

do {
     contentsOfFile = try String(contentsOfFile: "country", encoding: NSUTF8StringEncoding)
} catch {
    print(error);
    contentsOfFile = nil;
}

Also when you use a try you must have a do and catch

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
garanda
  • 1,271
  • 11
  • 16
  • 1
    The do and catch blocks are not required when using try. It is also possible to make an optional assignment based on the success of the operation: `var contentsOfFile = try? String(contentsOfFile: "country", encoding: NSUTF8StringEncoding)`. `contentsOfFile` will then be of the type `String?`. – Palle Mar 22 '16 at 22:55