zoukankan      html  css  js  c++  java
  • Swift 错误

    转载自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)")    
    } 
  • 相关阅读:
    Java判断一个字符是数字或字母
    java数组和字符串相互转换
    java 字符串截取的三种方法
    Templates && Algorithms
    挖坑——未完成题目列表QwQ
    作业_2018.08.25
    BZOJ1008 [HNOI2008]越狱 (快速幂,组合)
    UR #3 核聚变反应强度( gcd )
    A Super Hero
    NOIP2015 pj
  • 原文地址:https://www.cnblogs.com/ziyeSky/p/4130479.html
Copyright © 2011-2022 走看看