Swift 包中的结构声明在使用时导致编译错误
Struct declaration in Swift Package results in compilation error when used
当我将 IngredientStruct 声明为 swift 包的一部分时,在 RecipesVC.swift 中声明成分的行中出现此编译错误。但是,当我在项目中声明相同的结构时,我看不到这一点。这是代码片段:
RecipeStruct.swift // 这是在 swift 包 Recipes
中
import Foundation
public struct IngredientStruct: Encodable, Decodable, Identifiable {
public var id : String
public var recipeID : String
public var ingredientName : String
public var ingredientReplacement : String
public var measurementType : String
public var measurementQuantity : Int
public var storage : String
}
RecipesVC.swift
import SwiftUI
import Recipes // from Recipes swift package
struct RecipesVC: View {
var ingredient: IngredientStruct = IngredientStruct(id: "", recipeID: "", ingredientName: "", ingredientReplacement: "", measurementType: "", measurementQuantity: 0, storage: "") // "Extra arguments at positions #1, #2, #3, #4, #5, #6, #7 in call\n Missing argument for parameter 'from' in call \n Missing argument for parameter 'from' in call \n Insert 'from: <#Decoder#>, ‘”
var body: View {
…
}
}
现在,如果我在本地项目文件中声明相同的 IngredientStruct,它可以正常编译。我不确定我需要在 Swift 包中做什么才能进行编译。有什么指点吗?
@workingdog - 我的坏.. 匆忙剪切和粘贴时出错 - 在结构中丢失了 2 行以及同一行的第二个错误。我也将名称更新为小写。但是,我仍然看到相同的错误。
这是 Package.swift 文件:
import PackageDescription
let package = Package(
name: "Recipes",
platforms: [.iOS(.v15), .macOS(.v12)],
products: [
.library(
name: "Recipes",
targets: ["Recipes"]),
],
dependencies: [ ],
targets: [
.target(
name: "Recipes",
dependencies: []),
.testTarget(
name: "RecipesTests",
dependencies: ["Recipes"]),
]
)
如果正在创建结构 public,则必须对其进行适当的初始化。在上面的示例中,init() 看起来像这样:
public struct IngredientStruct: Encodable, Decodable, Identifiable {
public var id : String
public var recipeID : String
public var ingredientName : String
public var ingredientReplacement : String
public var measurementType : String
public var measurementQuantity : Int
public var storage : String
public init(id: String, recipeID: String, ingredientName: String, ingredientReplacement: String, measurementType: String, measurementQuantity: Int, storage: String) {
self.id = id
self.recipeID = recipeID
self.ingredientName = ingredientName
self.ingredientReplacement = ingredientReplacement
self.measurementType = measurementType
self.measurementQuantity = measurementQuantity
self.storage = storage
}
}
当我将 IngredientStruct 声明为 swift 包的一部分时,在 RecipesVC.swift 中声明成分的行中出现此编译错误。但是,当我在项目中声明相同的结构时,我看不到这一点。这是代码片段:
RecipeStruct.swift // 这是在 swift 包 Recipes
中import Foundation
public struct IngredientStruct: Encodable, Decodable, Identifiable {
public var id : String
public var recipeID : String
public var ingredientName : String
public var ingredientReplacement : String
public var measurementType : String
public var measurementQuantity : Int
public var storage : String
}
RecipesVC.swift
import SwiftUI
import Recipes // from Recipes swift package
struct RecipesVC: View {
var ingredient: IngredientStruct = IngredientStruct(id: "", recipeID: "", ingredientName: "", ingredientReplacement: "", measurementType: "", measurementQuantity: 0, storage: "") // "Extra arguments at positions #1, #2, #3, #4, #5, #6, #7 in call\n Missing argument for parameter 'from' in call \n Missing argument for parameter 'from' in call \n Insert 'from: <#Decoder#>, ‘”
var body: View {
…
}
}
现在,如果我在本地项目文件中声明相同的 IngredientStruct,它可以正常编译。我不确定我需要在 Swift 包中做什么才能进行编译。有什么指点吗?
@workingdog - 我的坏.. 匆忙剪切和粘贴时出错 - 在结构中丢失了 2 行以及同一行的第二个错误。我也将名称更新为小写。但是,我仍然看到相同的错误。
这是 Package.swift 文件:
import PackageDescription
let package = Package(
name: "Recipes",
platforms: [.iOS(.v15), .macOS(.v12)],
products: [
.library(
name: "Recipes",
targets: ["Recipes"]),
],
dependencies: [ ],
targets: [
.target(
name: "Recipes",
dependencies: []),
.testTarget(
name: "RecipesTests",
dependencies: ["Recipes"]),
]
)
如果正在创建结构 public,则必须对其进行适当的初始化。在上面的示例中,init() 看起来像这样:
public struct IngredientStruct: Encodable, Decodable, Identifiable {
public var id : String
public var recipeID : String
public var ingredientName : String
public var ingredientReplacement : String
public var measurementType : String
public var measurementQuantity : Int
public var storage : String
public init(id: String, recipeID: String, ingredientName: String, ingredientReplacement: String, measurementType: String, measurementQuantity: Int, storage: String) {
self.id = id
self.recipeID = recipeID
self.ingredientName = ingredientName
self.ingredientReplacement = ingredientReplacement
self.measurementType = measurementType
self.measurementQuantity = measurementQuantity
self.storage = storage
}
}