【问题标题】:Where did the text go?文字去哪儿了?
【发布时间】:2016-06-02 06:23:49
【问题描述】:

在你投反对票之前,我还没有发现任何类似的东西。

所以我制作了几个文本文件,每个文件都标有一个特定的数字(对应于月份中的哪一天)。每个文本文件都嵌入了一个引号。我有这样的情况,当在视图上按下按钮时,显示“历史”的标签会更改为该报价。

代码如下:

@IBAction func UpdateHistorical(sender: AnyObject) {//step 1
    let a = NSCalendar.currentCalendar().component(.Day, fromDate: NSDate())
    HistoricalLabel.text = String(a)
    var n = ""
    n = String(a)
    let hImage = UIImage.init(named: n)
    HistoricalImage.image = hImage

 //Below is the part that matters above was to just give context

    let path = NSTemporaryDirectory()+"1.txt"

    let readFile = try? String(contentsOfFile: path, encoding: NSUTF8StringEncoding)

    HistoricalLabel.text = readFile

}

}

这是视图的图像:

这是执行后视图的图像:

我做错了什么?

【问题讨论】:

  • 用 print() 语句检查你的字符串并确保它不为空
  • 哦,你说得对,它是空的。你对处理这个有什么建议吗?
  • 我觉得Here你可以找到解决办法
  • 你的文本文件在哪里?
  • @Desdenova 在右侧栏中与其他文本文件的文件夹中

标签: ios swift


【解决方案1】:

标签没有消失,但我认为您正在设置空字符串。读取文本文件时出现空字符串。

【讨论】:

  • 为什么?我的文本文件包含文本。抓取文字有什么问题吗?
  • path = NSTemporaryDirectory().stringByAppendingPathComponent(folder) path = path.stringByAppendingPathComponent(fileName + "." + fileType)
  • 让 fileLocation = NSBundle.mainBundle().pathForResource("filename", ofType: "txt")!让 text : String do { text = try String(contentsOfFile: fileLocation) } catch { text = "" }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2011-03-03
  • 2014-03-15
  • 2015-07-17
  • 2012-10-30
相关资源
最近更新 更多