- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我评论过 Apple 的:
Xcode 8 发行说明:
https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html
从 Swift 2.2 迁移到 Swift 2.3 或 Swift 3
https://swift.org/migration-guide/
macOS 10.12、iOS 10.0、tvOS 10.0 和 watchOS 3.0 中核心数据的新增功能
https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html#//apple_ref/doc/uid/TP40017342-CH1-DontLinkElementID_1
还有很多其他的...但是应该从 Apple 获得的一份文档,核心数据编程指南,却没有从 Swift 2 更新。
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html#//apple_ref/doc/uid/TP40001075-CH6-SW1
理想情况下,我正在寻找类似这样的东西,但对于 Swift 3。
https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
任何线索将不胜感激。
根据 Tom 的评论(下方)我缺少哪一步?
1) 创建一个新项目“测试”
2) 选择使用 CoreDate(这会创建 Test.xcdatamodeld)
这将使用以下内容自动填充 AppDelegate(删除默认注释):
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Test")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
3) 创建实体“Foo”
4) 添加属性“bar”类型字符串
5) 在 ViewController.swift 下添加以下内容(这是从 Apple 复制的,我只是将“...use”替换为“print”)
func findAnimals() {
let request: NSFetchRequest<Foo> = Foo.fetchRequest
do {
let searchResults = try context.fetch(request)
print(searchResults)
} catch {
print("Error with request: \(error)")
}
}
6) 在 override func viewDidLoad() 下添加 findAnimals()。
但是这有具体的错误:
7) 所以你返回并在 viewController 下的函数中添加一些东西以使容器可访问(这不是在 Apple 的示例中)。
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
很好,我们清除了 2 个错误中的 1 个,但错误“使用未声明的类型‘NSFetchRequest’”仍然存在。
这就是我卡住的地方。即使查阅了 Apple 发布的所有 Material ,我也找不到完整的示例。
最佳答案
@Aaron 再次感谢视频链接,它让我走上了正确的轨道。下面是在 Xcode 8 中使用 Swift 3 获取、添加和清除核心数据所需的最低限度的快速演练。
在类 ViewController 下添加以下内容:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext// Get data from he attributefunc getSample() { let request: NSFetchRequest = SampleEntity.fetchRequest() request.resultType = NSFetchRequestResultType.dictionaryResultType do { let searchResults = try context.fetch(request as! NSFetchRequest<NSFetchRequestResult>) as! [NSDictionary] let searchResultsArray = searchResults.map { $0["sampleAttribute"] as! String} print("searchResultsArray", searchResultsArray) } catch { print("Error with request: \(error)") }}// Save to the attributefunc setSample() { let saveSample = SampleEntity(context: context) saveSample.sampleAttribute = "Save a new string." do { try context.save() } catch { print("Error with save: \(error)") }}// clear the attributefunc resetSample() { let clearSample: NSFetchRequest = SampleEntity.fetchRequest() let deleteResults = NSBatchDeleteRequest(fetchRequest: clearSample as! NSFetchRequest<NSFetchRequestResult>) do { try context.execute(deleteResults) try context.save() } catch { print("Error with save: \(error)") }}
在 override func viewDidLoad() 下添加以下内容:
getSample()setSample()getSample()resetSample()getSample()
运行,会看到调试区打印如下:
searchResultsArray [] // Initially the attribute is emptysearchResultsArray ["Save new string."] // The attribute now contains the string searchResultsArray [] // This attribute has been cleared
关于ios - 在使用 Swift 3 的 Xcode 8 中寻找有关修订后的 NSPersistentContainer 的清晰教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39586084/
你好,我是 Cone。 首先,我们思考一个问题。 为什么会有操作系统? 在我们教科书上会提到分时系统、批处理系统等等现代操作系统前的中间产物,也会讲到管理硬件的功能,但似乎没有讲到为什么有
假设我只想清除顶部的所有条目,清除 Map 的 Map 的最合适方法是什么 -关卡 map ? Map> nestedMap; 方法A:仅清除顶层 map 。 nestedMap.clear();
我时不时遇到一个问题,我不确定解决方案是什么。 我有一个 2 列布局(左边是 strit,然后是主要区域)。在主要区域,我有时会有一个次要的 2 栏布局(例如 - 对于新闻部分,那里有一个图标,然后是
我有以下代码: Created Created .clearfix:after{ clear: both; bdy: "."; display
有没有办法CLS单行输出?我不相信 CLS 有任何开关,所以也许更好的问题是: 有什么办法吗 保留所有以前的输出以供重复使用? 或 捕获当前显示的输出(就像通过标记和复制一样)? 我只是想通过实时反馈
我有一个流式布局。当布局足够宽时,一些 div(.one 和 .two)可以全部水平排列在一条线上。 当布局最终被挤压时,右侧 float 的 div (.two) 最终会出现在多行上。有没有一种方法
我面临着一个我真的不知道从哪里开始解决的问题,所以我希望这个问题不要太宽泛。 我正在制作并在屏幕上应用它,我将有一个包含一些信息的矩形(假设它是一个 )并且我需要用另一个矩形覆盖那个矩形,所以当用户
我的 UITableView 跨越 iPhone View 的大小,并有一个 tableHeaderView 保存附加内容。想要标题清晰,表格的其余部分白色,我将表格的背景颜色设置为清晰,并在 Vie
我一直在使用 ImageMagick,但它产生的结果非常模糊。 convert -density 300 ../images/favicons/procensus.svg -background tr
我有手动数据的工作项目,但我想在我的项目中添加 json 解析。我认为我需要帮助。 (必须是实时解析,如果可能的话,新的item添加时会自动释放) 我的 TableView 代码 - (void)sc
我正在制作一个响应式网站。我有 3 个 div (.block),我需要将它们水平放置在一起。 当屏幕足够宽时,这很容易实现。但是,当我使浏览器更窄时,第三个 div (3) 换行到下一行,但我想要的
如何在 Swift 3 中使这个 UITableView 和它的单元格清晰。 我已经完成了前面的线程,但我仍然得到一个白色背景。 正如您从我的代码中看到的,我已经尝试了提到的各种方法: overrid
当我使用 为了显示图标,它在谷歌浏览器中看起来非常清晰锐利。然而,当我在 Firefox 或 Internet Explorer 中打开 svg 时,图标看起来很模糊。 这些浏览器似乎将图标呈现为半像
我是一名优秀的程序员,十分优秀!