Swift 2.2:类型'元素的价值' (又名' AnyObject')没有会员' objectID'

时间:2016-03-29 02:31:45

标签: ios swift core-data initialization

我有一段代码似乎在最新的Swift 2.2更新之前有效,但现在它不起作用。以下是相关的代码摘录。

        class Event {
        var eventObjectID:NSManagedObjectID?
        var eventDate:NSDate?
        var eventNotes:String?
        var eventType:String?

        init(eventObjectID: NSManagedObjectID, eventDate: NSDate, eventNotes: String, eventType: String) {
            self.eventObjectID = eventObjectID
            self.eventDate = eventDate
            self.eventNotes = eventNotes
            self.eventType = eventType
        }
    }


class EventsViewController: UIViewController {

        var eventsListArray:[Event] = [Event]()
        var selectedAquarium:Aquarium!
        var selectedFilters = [String]?()

        //Load Core Data into the noteListArray so that we can populate the table view.
        func setEventsListArray() {
            //Fetch all of the events from Core Data
            var results: NSArray = fetchFromCoreData("Event")

            //Sort the list so that the newest entries are on top
            let sortDescriptor = NSSortDescriptor(key: "eventDate", ascending: false)
            results = results.sortedArrayUsingDescriptors([sortDescriptor]) as NSArray

            //Find only the event entries that are related to this aquarium and match the selected filters
            if (results.count > 0) {
                for fetchResult in results {
                    let currentAquarium = fetchResult.valueForKey("aquarium") as! CoreDataAquarium

                    for selectedFilter in selectedFilters! {
                        let currentEventType = fetchResult.valueForKey("eventType") as! String

                        //Find only the events related to this aquarium and scrap the rest
                        if (currentAquarium.objectID == selectedAquarium.aquariumObjectID && currentEventType == selectedFilter) {

                            //"This event matches the filter..."

                            let newEvent:Event = Event(eventObjectID: fetchResult.objectID,
                                eventDate: fetchResult.valueForKey("eventDate") as! NSDate,
                                eventNotes: fetchResult.valueForKey("eventNotes") as! String,
                                eventType: fetchResult.valueForKey("eventType") as! String)

                            eventsListArray.append(newEvent)
                        }
                    }
                }
            } else {
                print("No results returned...")
            }
        }
}

我在初始化newEvent(接近代码末尾)的行中遇到以下两个错误:

  • 使用未声明的类型' NSManagedObjectID'
  • 类型'元素的价值' (又名' AnyObject')没有会员' objectID'

这些错误是否与新的Swift语言更新有关?或者我只是遗漏了一些非常明显的东西?

2 个答案:

答案 0 :(得分:2)

编译器无法识别术语NSManagedObject表明它并不了解核心数据框架。你需要说

import CoreData

(通常位于此文件的最顶部)。

答案 1 :(得分:1)

尽可能使用Swift本机集合类型。 List.Item1() 太未指定了。您不需要将数组转换为NSArray以便对其进行排序。

NSArray

这可能解决了类型问题。

PS:考虑使用//Fetch all of the events from Core Data var results = fetchFromCoreData("Event") //Sort the list so that the newest entries are on top results.sortInPlace {($0.valueForKey("eventDate") as! NSDate).compare($1.valueForKey("eventDate") as! NSDate) == .OrderedDescending } 子类来摆脱所有NSManagedObject个表达式