通知中心今天小部件闪烁然后 "unable to load" Swift

Notification center today widget flashes and then "unable to load" Swift

我正在开发一个简单的提醒应用程序,我 运行 在使用 Today Extension 时遇到了一些麻烦。该扩展在我的 iPad 和模拟器中完美运行,但是在我的 iPhone 上它在显示 "Unable to Load" 消息之前闪烁两次。我认为这是由某种内存问题引起的,因为在我的一个调试会话中调用了 didRecieveMemoryWarning,但通常我收到的唯一控制台输出是 "Program ended with exit code: 0." 任何帮助将不胜感激。这是我的 Today Extension 代码:

import UIKit
import NotificationCenter

class TodayViewController: UITableViewController, NCWidgetProviding {
let repeatTitles = ["No Repeat", "Repeat Every Minute", "Repeat Hourly", "Repeat Daily", "Repeat Weekly", "Repeat Monthly", "Repeat Annually", "Repeat Monthly Week Number", "Repeat Annual Week Number"]
var mainColor = UIColor(colorLiteralRed: 57/255, green: 181/255, blue: 74/255, alpha: 1.0)
var secondColor = UIColor(colorLiteralRed: 95/255, green: 147/255, blue: 196/255, alpha: 1.0)

var objects = [NSArray]()
override func viewDidLoad() {
    super.viewDidLoad()

    }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    print("MEMORY WARNING NC")

}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let object = objects[indexPath.row] as NSArray
    if object[7] as! String == "CREATEONE" || object[7] as! String == "MOREMEMOS" {
        cell.detailTextLabel!.text = object[2] as? String
        cell.detailTextLabel!.textColor = secondColor
        cell.detailTextLabel!.font = UIFont.systemFontOfSize(16)
    }
    else {
        cell.textLabel!.text = object[2] as? String
        if let theDateToStore = object[4] as? NSDate {
            if object[6] as! Bool == true {
                if (object[3] as! String).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) == "" {
                    cell.detailTextLabel!.text = "Memo Muted"
                }
                else {
                    cell.detailTextLabel!.text = "Memo Muted- \(object[3] as! String)"
                }
            }
            else {
                let timestamp = NSDateFormatter.localizedStringFromDate(theDateToStore, dateStyle: .ShortStyle, timeStyle: .ShortStyle)
                let repeatTitle = repeatTitles[(object[5] as? Int)!]
                if (object[3] as! String).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) == "" {
                    cell.detailTextLabel!.text = "\(timestamp): \(repeatTitle)"
                }
                else {
                    cell.detailTextLabel!.text = "\(timestamp): \(repeatTitle)- \(object[3] as! String)"
                }
            }
        }
        else {
            if (object[3] as! String).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) == "" {
                cell.detailTextLabel!.text = "Memo Muted"
            }
            else {
                cell.detailTextLabel!.text = "Memo Muted- \(object[3] as! String)"
            }
        }
        cell.textLabel!.textColor = mainColor
        cell.detailTextLabel!.textColor = UIColor.whiteColor()
    }
    return cell
}
func updateWithChanges(){
    var newObjects = NSUserDefaults(suiteName: "group.Me.MemosApp")!.objectForKey("cashedMemos") as? [NSArray]
    if newObjects == nil {
        newObjects = [NSArray]()
    }
    else {
        if newObjects!.count > 5 {
            for i in stride(from: newObjects!.count - 1, through: 5, by: -1) {
                newObjects!.removeAtIndex(i)
            }
            newObjects!.append([0, true, "More Memos", "", "", 0, false, "MOREMEMOS"])
        }
    }
    newObjects!.append([0, true, "Create a New Memo", "", "", 0, false, "CREATEONE"])
    objects = newObjects!
    newObjects = nil
    tableView.reloadData()
    updatePreferredContentSize()
}

override func viewWillAppear(animated: Bool) {
    updateWithChanges()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let object = objects[indexPath.row] as NSArray? {
        if object[7] as! String == "CREATEONE" {
            self.extensionContext?.openURL(NSURL(string: "MemosApp://createMemo")!, completionHandler: nil)
        }
        else if object[7] as! String == "MOREMEMOS" {
            self.extensionContext?.openURL(NSURL(string: "MemosApp://moreMemosFromNC")!, completionHandler: nil)
        }
        else {
            let idNum = object[0] as! Int
            let deviceID = object[7] as! String
            let memoID = "m\(idNum)\(deviceID)"
            self.extensionContext?.openURL(NSURL(string: "MemosApp://openMemo?\(memoID)")!, completionHandler: nil)
        }
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    completionHandler(NCUpdateResult.NoData)
}
func widgetMarginInsetsForProposedMarginInsets
    (defaultMarginInsets: UIEdgeInsets) -> (UIEdgeInsets) {
        return UIEdgeInsetsZero
}
func updatePreferredContentSize() {
    preferredContentSize = CGSizeMake(CGFloat(0), CGFloat((objects.count * 50) - 1))//+ tableView.sectionFooterHeight)
}
}

Today Extension 从 NSUserDefaults 共享组中的一个对象加载一个已兑现的备忘录数组。代码写在Swift 2.0.

我注意到您对 colorLiteralRed 使用了整数。这是你想要的吗?

原来我在 tableView 的基本单元格中创建了一个额外的 UILabel(尽管在 NC 中有透明背景,但用于使单元格可点击)并且这个标签的长度导致小部件使用内存太大。