Swift プロジェクトに問題があります。薬を飲む必要があるときに通知できるアプリを作成しています。
エラーコードが生成されるファイルは次のとおりです。
import Foundation
import UIKit
class TodoList {
class var sharedInstance : TodoList {
struct Static {
static let instance : TodoList = TodoList()
}
return Static.instance
}
private let ITEMS_KEY = "todoItems"
func allItems() -> [TodoItem] {
let todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? [:]
let items = Array(todoDictionary.values)
return items.map({TodoItem(deadline: $0["deadline"] as! NSDate, title: $0["title"] as! String, gentag: $0 ["gentag"] as! RepeatInterval, UUID: $0["UUID"] as! String)}).sort({(left: TodoItem, right:TodoItem) -> Bool in
(left.deadline.compare(right.deadline) == .OrderedAscending)
})
}
func addItem(item: TodoItem) {
// persist a representation of this todo item in NSUserDefaults
var todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? Dictionary() // if todoItems hasn't been set in user defaults, initialize todoDictionary to an empty dictionary using nil-coalescing operator (??)
todoDictionary[item.UUID] = ["deadline": item.deadline, "title": item.title, "gentag": item.gentag.toCalendarUnit(), "UUID": item.UUID] // store NSData representation of todo item in dictionary with UUID as key
NSUserDefaults.standardUserDefaults().setObject(todoDictionary, forKey: ITEMS_KEY) // save/overwrite todo item list
// create a corresponding local notification
let notification = UILocalNotification()
notification.alertBody = "Hej \"\(item.title)\"Test, husk at tage din medicin: \"\(item.title)\"" // text that will be displayed in the notification
notification.alertAction = "Skub for at tage medicin" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = item.deadline // todo item due date (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.userInfo = ["title": item.title, "UUID": item.UUID] // assign a unique identifier to the notification so that we can retrieve it later
notification.repeatInterval = item.gentag.toCalendarUnit()
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func removeItem(item: TodoItem) {
for notification in UIApplication.sharedApplication().scheduledLocalNotifications as [UILocalNotification]! { // loop through notifications...
if (notification.userInfo!["UUID"] as! String == item.UUID) { // ...and cancel the notification that corresponds to this TodoItem instance (matched by UUID)
UIApplication.sharedApplication().cancelLocalNotification(notification) // there should be a maximum of one match on UUID
break
}
}
if var todoItems = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) {
todoItems.removeValueForKey(item.UUID)
NSUserDefaults.standardUserDefaults().setObject(todoItems, forKey: ITEMS_KEY) // save/overwrite todo item list
}
}
エラーを生成する行は次のとおりです。
todoDictionary[item.UUID] = ["deadline": item.deadline, "title": item.title, "gentag": item.gentag.toCalendarUnit(), "UUID": item.UUID] // store NSData representation of todo item in dictionary with UUID as key
エラーテキストは次のとおりです。
コンテキスト タイプ 'AnyObject' は辞書リテラルでは使用できません
ここに私の ToDoItem クラスがあります
import Foundation
struct TodoItem {
var title: String
var deadline: NSDate
var gentag: RepeatInterval
var UUID: String
init(deadline: NSDate, title: String, gentag: RepeatInterval, UUID: String) {
self.deadline = deadline
self.title = title
self.gentag = gentag
self.UUID = UUID
}
var isOverdue: Bool {
return (NSDate().compare(self.deadline) == NSComparisonResult.OrderedDescending) // deadline is earlier than current date
}
}