Playground 你不知道的小技巧, CoreData 的使用
2017-03-02 17:54
232 查看
Playground 的出现无疑是大大的提高了开发效率,可以节省大量的编译时间。这里介绍在 Playground 中使用 CoreData 的小技巧。我们新建一个工程 iOS 项目工程。
点击 File -> New -> File , 在工程中新建文件 Data Model 文件
![](http://upload-images.jianshu.io/upload_images/1745132-7091887265862684.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在 model 中添加一个 Entitle,如下图
![](http://upload-images.jianshu.io/upload_images/1745132-e9a0fc91c6892711.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
编译工程后,在 Product 选择生成的 .app 文件,找到该目录,如下图
![](http://upload-images.jianshu.io/upload_images/1745132-4af44d627b106237.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
查看包中的文件,如图
![](http://upload-images.jianshu.io/upload_images/1745132-3164965ec87a8c00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到一个 Mode.momd 文件, 如图
![](http://upload-images.jianshu.io/upload_images/1745132-65d04b35d08430b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在工程中新建一个 playground 文件
![](http://upload-images.jianshu.io/upload_images/1745132-272c3f1e30477cdb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
把刚才的 Model.momd 文件拷贝到 playground 的 Resource 目录下
![](http://upload-images.jianshu.io/upload_images/1745132-c9967fec13dbcc02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在 playground 中就可以直接使用这个 Model 资源了
![](http://upload-images.jianshu.io/upload_images/1745132-9a306b37e1ba23bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
参考链接作者: HuminiOS - 极光( JPush 为极光团队账号,欢迎关注)
原文: http://www.jianshu.com/p/818c063cf686
知乎专栏:极光日报
点击 File -> New -> File , 在工程中新建文件 Data Model 文件
![](http://upload-images.jianshu.io/upload_images/1745132-7091887265862684.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在 model 中添加一个 Entitle,如下图
![](http://upload-images.jianshu.io/upload_images/1745132-e9a0fc91c6892711.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
编译工程后,在 Product 选择生成的 .app 文件,找到该目录,如下图
![](http://upload-images.jianshu.io/upload_images/1745132-4af44d627b106237.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
查看包中的文件,如图
![](http://upload-images.jianshu.io/upload_images/1745132-3164965ec87a8c00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到一个 Mode.momd 文件, 如图
![](http://upload-images.jianshu.io/upload_images/1745132-65d04b35d08430b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在工程中新建一个 playground 文件
![](http://upload-images.jianshu.io/upload_images/1745132-272c3f1e30477cdb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
把刚才的 Model.momd 文件拷贝到 playground 的 Resource 目录下
![](http://upload-images.jianshu.io/upload_images/1745132-c9967fec13dbcc02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在 playground 中就可以直接使用这个 Model 资源了
//: Playground - noun: a place where people can playimport UIKitimport CoreData// Core Data Stack Setup for In-Memory Storepublic 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 Entitylet ent = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context) ent.setValue("fasf", forKey: "name")try! context.save()// Perform a fetch requestlet fr = NSFetchRequest(entityName: "Entity")let result = try! context.executeFetchRequest(fr)print(result)结果如图
![](http://upload-images.jianshu.io/upload_images/1745132-9a306b37e1ba23bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
参考链接作者: HuminiOS - 极光( JPush 为极光团队账号,欢迎关注)
原文: http://www.jianshu.com/p/818c063cf686
知乎专栏:极光日报
相关文章推荐
- Playground 你不知道的小技巧, CoreData 的使用
- Playground 你不知道的小技巧, CoreData 的使用
- Playground 你不知道的小技巧, CoreData 的使用
- Playground 你不知道的小技巧,CoreData的使用
- IOS成长之路-Core Data使用时用到的类--微解
- [Cocoa]深入浅出 Cocoa 之 Core Data(3)- 使用绑定
- ios简单使用core data
- 使用core data
- [Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定
- 使用coredata导致的版本更新后程序crash的问题
- IOS成长之路-CoreData使用
- [Cocoa]深入浅出Cocoa之Core Data(4)- 使用绑定
- 使用coredata导致的版本更新后程序crash的问题
- IOS6 编程:Core Data持久化数据存储(5)-使用Core Data模板创建EntLibCart项目
- 深入浅出 Cocoa 之 Core Data(3)- 使用绑定
- [ios开发基础之 Core Data[3]]如何使用NSFetchedResultsController 来集成TableView操作
- 使用coredata导致的版本更新后程序crash的问题
- [Cocoa]深入浅出 Cocoa 之 Core Data(3)- 使用绑定
- [Cocoa]深入浅出 Cocoa 之 Core Data(3)- 使用绑定
- xcode4.3 coredata使用时报Code=134100 错误的解决方法