
























https://developer.apple.com/documentation/swiftdata
@Model 注解 修饰器
import SwiftUI
import SwiftData
@Model
class RemoteImage {
var sourceURL: URL
var data: Data
init(sourceURL: URL, data: Data = Data()) {
self.sourceURL = sourceURL
self.data = data
}
}
expand marco

import SwiftUI
import SwiftData
@Model
class RemoteImage {
@_PersistedProperty
var sourceURL: URL
@_PersistedProperty
var data: Data
init(sourceURL: URL, data: Data = Data()) {
self.sourceURL = sourceURL
self.data = data
}
@Transient
private var _$backingData: any SwiftData.BackingData<RemoteImage> = RemoteImage.createBackingData()
public var persistentBackingData: any SwiftData.BackingData<RemoteImage> {
get {
return _$backingData
}
set {
_$backingData = newValue
}
}
static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] {
return [
SwiftData.Schema.PropertyMetadata(name: "sourceURL", keypath: \RemoteImage.sourceURL, defaultValue: nil, metadata: nil),
SwiftData.Schema.PropertyMetadata(name: "data", keypath: \RemoteImage.data, defaultValue: nil, metadata: nil)
]
}
required init(backingData: any SwiftData.BackingData<RemoteImage>) {
_sourceURL = _SwiftDataNoType()
_data = _SwiftDataNoType()
self.persistentBackingData = backingData
}
@Transient private let _$observationRegistrar = Observation.ObservationRegistrar()
internal nonisolated func access<_M>(
keyPath: KeyPath<RemoteImage, _M>
) {
_$observationRegistrar.access(self, keyPath: keyPath)
}
internal nonisolated func withMutation<_M, _MR>(
keyPath: KeyPath<RemoteImage, _M>,
_ mutation: () throws -> _MR
) rethrows -> _MR {
try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
struct _SwiftDataNoType {
}
}
extension RemoteImage: SwiftData.PersistentModel {
}
extension RemoteImage: Observation.Observable {
}
https://developer.apple.com/documentation/swiftdata/model()
https://developer.apple.com/documentation/swiftdata/persistentmodel



https://developer.apple.com/documentation/coredata

.xcdatamodeld
https://developer.apple.com/documentation/coredata/creating-a-core-data-model
SwiftData vs Core DataSwiftData 新版
Core Data 旧版
混合版,保留 Core Data + 渐进式 SwiftData

https://developer.apple.com/documentation/CoreData/adopting-swiftdata-for-a-core-data-app
https://developer.apple.com/documentation/coredata/core-data-model
@attached(member, conformances: Observable, PersistentModel, Sendable, names: named(_\(backingData), named(persistentBackingData), named(schemaMetadata), named(init), named(_\)observationRegistrar), named(_SwiftDataNoType), named(access), named(withMutation)) @attached(memberAttribute) @attached(extension, conformances: Observable, PersistentModel, Sendable) macro Model()
// provide conformance to the PersistentModel and Observable protocols.
@Model
class RemoteImage {
var sourceURL: URL
var data: Data
init(sourceURL: URL, data: Data = Data()) {
self.sourceURL = sourceURL
self.data = data
}
}
import SwiftData
// Annotate new or existing model classes with the @Model macro.
@Model
class Trip {
var name: String
var destination: String
var startDate: Date
var endDate: Date
var accommodation: Accommodation?
}
https://developer.apple.com/documentation/swiftdata/preserving-your-apps-model-data-across-launches
https://developer.apple.com/documentation/Observation/Observable
https://developer.apple.com/documentation/Swift/Codable
Storyboard vs SwiftUIStoryboard 可以选择 Swift / Objective-C

Storyboard 只能使用 Core Data


Objective-C 测试只能选择 XCTest

Swift 测试可以选择 XCTest / Swift Testing

SwiftUI 必须使用 Swift

SwiftUI 可以选择 SwiftData / Core Data

Swift 测试可以选择 XCTest / Swift Testing

https://www.hackingwithswift.com/quick-start/swiftdata
https://www.hackingwithswift.com/quick-start/swiftdata/swiftdata-vs-core-data
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。