简体   繁体   English

swift2将文本文件的内容加载到变量中

[英]swift2 loading contents of text file into variable

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")

The try is not in the right position: try位置不正确:

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 此外,当你使用一个尝试,你必须有一个docatch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM