您的位置:首页 > 移动开发 > Swift

coredata_使用CoreData的Swift / SwiftUI的初学者建议(1)

2020-08-23 22:07 1541 查看

coredata

Code-Generation and Class-Extensions

代码生成和类扩展

Introduction:

介绍:

For several weeks now, I have been learning Swift 5 with SwiftUI and came across many tutorials and examples that show only the fastest but not necessarily the most sustainable way. With this article I want to show the differences between the fast and sustainable approaches. I want to point out, that I’m still a beginner and that there are certainly much better ways to reach your goal. Nevertheless, I would like to describe my approach, since I as a beginner understand this approach well and therefore think that other beginners may also profit from this sustainable method.

几周以来,我一直在使用SwiftUI学习Swift 5,并且遇到了许多教程和示例,这些教程和示例仅显示了最快但不一定是最可持续的方式。 在本文中,我想展示快速方法与可持续方法之间的差异。 我想指出的是,我仍然是一个初学者,当然还有更好的方法可以实现您的目标。 但是,我想描述一下我的方法,因为我作为一个初学者非常了解这种方法,因此认为其他初学者也可以从这种可持续的方法中受益。

XCode/CoreData “Code generation”:

XCode / CoreData“代码生成”:

There is a variety of examples for Core Data with Swift/SwiftUI that show how to set up the entities and attributes. In most cases it is recommended to set the code generation to “Manual/None”.

使用Swift / SwiftUI的核心数据有很多示例,展示了如何设置实体和属性。 在大多数情况下,建议将代码生成设置为“手动/无”。

Ess MartinEss Martin建模的SCREENSHOT XCode设置Projectname.xcdata

This recommendation is probably based on the assumption that the configuration of the entities and attributes is done once and is not continuously extended and changed. However, this is hardly the case in real projects. In real projects, the configuration will often be changed and optimized. As a result, the classes have to be generated manually very often.

该建议可能基于以下假设:实体和属性的配置仅执行一次,并且不会连续扩展和更改。 但是,在实际项目中几乎不是这种情况。 在实际项目中,通常会更改和优化配置。 结果,必须非常频繁地手动生成类。

My suggestion is to set this setting to “Class Definition”. In this case the attributes are managed automatically and are therefore, always up to date. This procedure saves you a lot of unnecessary work. In addition, this prevents the accidental deletion of extensions that have been made in the existing class (extensions).

我的建议是将此设置设置为“ Class Definition”。 在这种情况下,属性是自动管理的,因此始终是最新的。 此过程为您节省了很多不必要的工作。 此外,这可以防止意外删除在现有类中进行的扩展(扩展)。

XCode/CoreData “Class extensions”:

XCode / CoreData“类扩展”:

When it comes to class extensions, the approach in most examples is not focused on sustainability. In most cases, the generated class extension is expanded manually. With the setting “Manual/None”, the extensions have to be migrated manually each time, creating unnecessary steps and a possible source of error.

当涉及到课程扩展时,大多数示例中的方法都不是针对可持续性的。 在大多数情况下,将手动扩展生成的类扩展。 使用“手动/无”设置,每次都必须手动迁移扩展,从而造成不必要的步骤和可能的错误源。

Martin EssMartin Ess自行更改

My suggestion is to always create a separate file with the additional extensions, regardless of the code generation settings. This file will then remain unaffected by the regenerations. This approach makes it possible to replace the two existing generated files with the newly generated ones promptly.

我的建议是,无论代码生成设置如何,始终创建带有附加扩展名的单独文件。 然后,该文件将不受重新生成的影响。 这种方法使得可以用新生成的文件Swift替换两个现有的生成文件。

Martin EssMartin Ess的自写扩展(示例)

If you have any suggestions or ideas, I would be happy to hear from you and exchange opinions.

如果您有任何建议或想法,我们将很高兴收到您的来信并交换意见。

翻译自: https://medium.com/@essmartin/beginners-advice-for-swift-swiftui-with-coredata-1-61407adbc597

coredata

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: