爱程序网

Swift 错误 - Swift开发coredata出现Class not found解决办法

来源: 阅读:

转载自http://blog.csdn.net/longhai0long/article/details/38379247

刚看swift开发IOS,在使用Core Data框架时遇到了一个问题,控制台打出的log 如下:

CoreData: warning: Unable to load class named 'CollectNews' for entity 'CollectNews'.  Class not found, using default NSManagedObject instead.

百思不得其解抓狂,后来在StackOverFlow上找到了一种解决办法,就是在Model类的开关加上这个的注解:

@objc(CollectNews) 

Demo的部分代码分享给大家,仅做参考:
Model类

import Foundation    
import CoreData  
@objc(CollectNews)    
class CollectNews: NSManagedObject {      
    @NSManaged var date: NSNumber    
    @NSManaged var newsId: NSNumber    
} 

AppDelegate类:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {      
    insertData()    
    selectData()      
    return true    
}    
    
//插入数据    
func insertData(){      
    //取Entity对象    
    var collectNews = NSEntityDescription.insertNewObjectForEntityForName("CollectNews", inManagedObjectContext: self.managedObjectContext) as CollectNews    
    
    //对属性进行修改    
    collectNews.newsId = 12    
    collectNews.date = 20140805    
    
    //保存    
    if self.managedObjectContext.hasChanges{    
        self.managedObjectContext.save(nil)    
    }    
}    
    
//查询    
func selectData(){    
    //得request对象    
    var request = NSFetchRequest()    
    var entityQueue = NSEntityDescription.entityForName("CollectNews", inManagedObjectContext: self.managedObjectContext)    
    request.entity = entityQueue    
    var errorInfo:NSError?    
    //取结果集    
    var resultArr = self.managedObjectContext.executeFetchRequest(request, error: &errorInfo) as NSArray    
    
    println("----》\(resultArr.count)")    
    
    var temp = resultArr[0] as CollectNews    
    
    println("--->\(temp.newsId)")    
} 

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助