zoukankan      html  css  js  c++  java
  • SWIFT 之CoreData初试

    SWIFT中使用CoreData来保存本地数据,在建立项目的时候把 "Use Core Data"选项选上

    项目建立完成后点击后缀为 .xcdatamodeld的那个文件,点击右下角"Add Entity"添加一个Entity后可以修改其名称,接着在"Attributes"下面点击“+”号添加一个

    Attribute

    接着就可以上代码操作了,首先先添加引用

    import CoreData

    //It's necessary to code these two rows if you want to use CoreData

    var applicationDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    var managedObjectContext = applicationDelegate.managedObjectContext

    //Get the entity by entityName        

    var entity = NSEntityDescription.entityForName("Notes", inManagedObjectContext: managedObjectContext!)

    //Get the ManagedObject

    var title = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

    //Set the ManagedObject Value for key

    title.setValue(text, forKey: "title")

    var error: NSError?

    //Save content

    if(managedObjectContext?.save(&error) == nil){

     }

    //Get data from the CoreData

    var applicationDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    var managedObjectContext = applicationDelegate.managedObjectContext

    var fetchRequest = NSFetchRequest(entityName: "Notes")

            

    var error:NSError?

    var fetchResults = managedObjectContext?.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?

    if let results = fetchResults{

          var  notes = results

    }else{

            println(error)

    }

  • 相关阅读:
    WPF通过不透明蒙板切割显示子控件
    WPF图片,DataGrid等实现圆角
    今天是2015年1月5日
    控制反转&依赖注入
    如何提高sql查询性能到达优化程序的目的
    handle句柄
    winform加快窗体加载速度
    winform窗体自适应大小
    快速整理代码(c#)
    IC卡、ID卡、M1卡、射频卡的区别是什么(射频卡是种通信技术)
  • 原文地址:https://www.cnblogs.com/foxting/p/4471357.html
Copyright © 2011-2022 走看看