游戏技术文章

Playground 你不知道的小技巧, CoreData 的使用

时间:2017-3-9 13:16:37  作者:棋牌资源网  来源:棋牌资源网  查看:7725  评论:0
内容摘要:Playground 的出现无疑是大大的提高了开发效率,可以节省大量的编译时间。这里介绍在 Playground 中使用 CoreData 的小技巧。我们新建一个工程 iOS 项目工程。点击File -> New -> File , 在工程中新建文件 Data Mod...
Playground 的出现无疑是大大的提高了开发效率,可以节省大量的编译时间。

这里介绍在 Playground 中使用 CoreData 的小技巧。

  1. 我们新建一个工程 iOS 项目工程。
  2. 点击File -> New -> File , 在工程中新建文件 Data Model 文件
  3. 在 model 中添加一个 Entitle,如下图
  4. 编译工程后,在 Product 选择生成的 .app 文件,找到该目录,如下图
  5. 查看包中的文件,如图
  6. 可以看到一个 Mode.momd 文件, 如图
  7. 在工程中新建一个 playground 文件
  8. 把刚才的 Model.momd 文件拷贝到 playground 的 Resource 目录下
  9. 在 playground 中就可以直接使用这个 Model 资源了
//: Playground - noun: a place where people can play

import UIKit
import CoreData

// Core Data Stack Setup for In-Memory Store
public func getModelContext(name:String) -> NSManagedObjectContext {
  
  // Replace "Model" with the name of your model
  let modelUrl = NSBundle.mainBundle().URLForResource(name, withExtension: "momd")
  guard let model = NSManagedObjectModel.init(contentsOfURL: modelUrl!) else { fatalError("not this file") }
  
  let psc = NSPersistentStoreCoordinator(managedObjectModel: model)
  try! psc.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil)
  
  let context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
  context.persistentStoreCoordinator = psc
  
  return context
}

let context = getModelContext("Model")

// Insert a new Entity
let ent = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context)
ent.setValue("fasf", forKey: "name")

try! context.save()

// Perform a fetch request
let fr = NSFetchRequest(entityName: "Entity")
let result = try! context.executeFetchRequest(fr)

print(result)

结果如图

参考链接

标签:Playground你不知道的小技巧 CoreData的使用 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved