码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-29 13:04:04      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

转载自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.

百思不得其解bubuko.com,布布扣,后来在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)")    
} 

 

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

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/ziyeSky/p/4130479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!