我添加了一个今天的扩展我的应用程序,直到特定的代码行被编译这一切工作正常。注:编译,不执行!
我TodayViewController
是:
class StoredDoses {
func getDoses(doses: inout [Dose]) {
if let userD = UserDefaults(suiteName: "com.btv.mySuite") {
if let dosesData = userD.object(forKey: "doses_key") {
do {
// -----------------------------------------------
// Comment the line below out and the widget works
doses = try PropertyListDecoder().decode([Dose].self, from: dosesData as! Data)
// -----------------------------------------------
} catch {
print ("ERROR")
}
}
}
}
}
class TodayViewController: UIViewController, NCWidgetProviding {
@IBOutlet weak var aText: UILabel!
@IBOutlet weak var bText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
//Just for development stage - not real, final code
let form = DateFormatter()
form.timeStyle = .short
aText.text = form.string(from: Date())
completionHandler(NCUpdateResult.newData)
}
}
所以,上面的代码写得不好,但它是我用过终于缩小卸载控件的原因。 Doses
的阵列是一个自定义,可编码类,但如果我试图让String
数组,然后它是相同的。该StoredDoses
代码包含在主要的应用程序,并不会造成任何问题。
只是再次重申:我不是要执行的StoredDoses
类的任何方法。我甚至不具备的小工具的一个实例。当doses = ...
线只是注释掉然后小部件的负荷,并在插件的aText
标签出现在它的当前时间。
好了,感谢@克里斯显然无关劝我把它整理!
这似乎是一个Interface Builder的问题:不知它保留了,这是自动创建的,当我加在Xcode今日延伸UILabel
的原始名称。在某些时候,在它的IBOutlet
用的“Hello World”连接到标签后,我就改名为略多相关的东西,但曾在TodayViewController
不无关系之前过度输入新的名称。
控制台没有抛出了任何问题,有时似乎工作,但与线
try PropertyListDecoder().decode([Dose].self, from: dosesData as! Data)
是存在,那么它停止没有任何控制台消息的工作。
我只发现了这一点后,我探讨关于as! Data
@克里斯评论。我重新写首先得到的数据:
if let userD = UserDefaults(suiteName: "com.btv.mySuite") {
if let dosesData = userD.object(forKey: "doses_key") {
if let unwrappedData = dosesData as? Data {
do {
doses = try PropertyListDecoder().decode([SplitDose].self, from: unwrappedData)
} catch {
doses.removeAll()
}
}
}
}
一旦被编译(请记住,它仍然没有被执行 - 这只是坐在那里等待被使用)控制台吐出来了一条消息,并且应用程序折戟显出老UILabel
名not key-compliant
。重新连接在IB固定一切UILabel
,我可以编译原码....
这可能值得雷达条目,但现在我不想浪费任何一天重新创建(如果可能的话)这个问题!