上下文类型“AnyObject”不能与字典文字一起使用

时间:2016-06-26 07:13:33

标签: ios xcode swift anyobject

我的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
    }
}

1 个答案:

答案 0 :(得分:0)

我无法重现你的麻烦......

import Foundation

struct S {
    var i = 1
    var b = true
    var c = 1.0
    var id = "XYZ-ID"
}

var d = NSUserDefaults.standardUserDefaults().dictionaryForKey("dummy_key") ?? [:]
let s = S()
d[s.id] = ["i":s.i, "b":s.b, "c":s.c]


print(d)
/*
 ["XYZ-ID": {
 b = 1;
 c = 1;
 i = 1;
 }]
*/

您收到的错误意味着,swift无法在作业的右侧创建AnyObject。单独检查,很可能是因为RepeatInterval没有桥接基础对应物。