是否应该使用类继承来破坏类的可解码性.例如,以下代码
class Server : Codable {
var id : Int?
}
class Development : Server {
var name : String?
var userId : Int?
}
var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}"
let jsonDecoder = JSONDecoder()
let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development
print(item.id ?? "id is nil")
print(item.name ?? "name is nil") here
Run Code Online (Sandbox Code Playgroud)
输出是:
1
name is nil
Run Code Online (Sandbox Code Playgroud)
现在,如果我反转这个,名称解码但id没有.
class Server {
var id : Int?
}
class Development : Server, Codable {
var …Run Code Online (Sandbox Code Playgroud) 我有以下总体架构。一个为我的所有模型数据导入 Swift 包的应用程序,然后根据平台与 Firestore 一起使用或直接 REST 到 firestore。我对 WatchOS 部分使用 REST,因为 firestore-swift 仍然不支持 WatchOS。
我的 Swift 包依赖于 Firestore,这不是问题。但是,我只希望目标部分中的目标可用于 iOS 和 MacOS,而不包含在 WatchOS 上。我可以在包清单文件中指定它吗?
这是我正在尝试做的事情的总体图表。
我的包清单如下所示。请注意底部的两行注释掉的行,我只想在目标是 iOS 或 MacOS 时才包含它们。
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "EMobileLibrary",
platforms: [.iOS(.v15), .watchOS(.v8), .macOS(.v12)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "EMobileLibrary",
targets: …Run Code Online (Sandbox Code Playgroud) 我看到评论说这不受支持,但它们已经存在一年多了。最新的 Swift 和 Xcode 13 是否改变了这一点?
显然你可以在目标部分执行此操作
targets: [
.target(
name: "MyLibrary",
swiftSettings: [
.define("MYCONDITIONVARIABLE", .when(configuration: .debug)),
]),
Run Code Online (Sandbox Code Playgroud)
但是,我想指定除调试和发布之外的其他构建方案。