• 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)

    }

  • 相关阅读:
    c++ 堆和栈的区别 ,sizeof详解以及sizeof与strlen的区别,memset和fill的比较 木
    python 好文收集 木
    codeforces 285 D Permutation Sum (状态压缩DP) 木
    ural 1133. Fibonacci Sequence 木
    pythyon 集合和字典 2.7 木
    python 栈和队列 排序 初级数据结构 木
    【考试总结】20220321
    【考试总结】20220315
    【2022March】杂题乱写
    Codeforces1365G Secure Password
  • 原文地址:https://www.cnblogs.com/foxting/p/4471357.html
Copyright © 2020-2023  润新知