gpt4 book ai didi

swift - config.preparations.append(modelName.self) 错误使用未解析的标识符 'modelName'

转载 作者:行者123 更新时间:2023-11-28 15:18:33 26 4
gpt4 key购买 nike

我已经通过流利的提供者的正确方法使用 vapor(服务器端)PostgreSQL 为我的模型(Swift) 建立关系provider(database),我按照fluent的一般方法,但是不知道哪里做错了,在extension for modelName的准备上,在我的 modelName.swiftma​​in.swift 的代码下面。

 import Foundation
import Vapor
import FluentProvider
import PostgreSQLProvider


final class modelName: Model {


let storage = Storage()
var id: Node?
var name:String
var displayName:String
public var content: String

init(content: String, displayName:String, name:String) {

self.content = content
self.displayName = displayName
self.name = name
}

func forDataBase() {


let array:[berichtDataPoint] = [intDataPoint(), boolDataPoint(), doubleDataPoint()]

let _ = array[0] as! intDataPoint
let _ = array[1] as! doubleDataPoint


for point in array {

switch point {
case is intDataPoint:
print("int")
case is doubleDataPoint:
print("double")
case is boolDataPoint:
print("bool")
default:
print("error")
}
}

}

func makeRow() throws -> Row {
var row = Row()
try row.set("id", idKey)
try row.set("displayName", displayName)
try row.set("name", name)
return row
}

init(row: Row) throws {
content = try row.get("content")
displayName = try row.get("displayName")
name = try row.get("name")
}

func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"content": content,
"displayName": displayName,
"name": name
])
}
}


extension modelName: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { modelName in
modelName.id()
modelName.string("displayName")
modelName.string("name")

}
}

static func revert(_ database: Database) throws {
try database.delete(self)
}
}

ma​​in.swift

import App
import Vapor
import FluentProvider
import PostgreSQLProvider

/// We have isolated all of our App's logic into
/// the App module because it makes our app
/// more testable.
///
/// In general, the executable portion of our App
/// shouldn't include much more code than is presented
/// here.
///
/// We simply initialize our Droplet, optionally
/// passing in values if necessary
/// Then, we pass it to our App's setup function
/// this should setup all the routes and special
/// features of our app
///
/// .run() runs the Droplet's commands,
/// if no command is given, it will default to "serve"
let config = try Config()
config.preparations.append(modelName.self) \\error is here '(Use of
unresolved identifier 'modelName')

let drop = try Droplet(config)
try drop.setup()

try drop.run()

最佳答案

我认为根本原因是模块是分开的。如果您将 vapor 项目创建为 vapor newmain.swiftRun 模块中,modelName.swiftApp 模块。

// Package.swift

let package = Package(
name: "hello",
targets: [
Target(name: "App"),
Target(name: "Run", dependencies: ["App"]),
],

当访问其他模块类时,目标类的访问级别必须使用openpublic

// modelName.swift

public class moduleName: Model {
...

请注意,您还必须根据此更改修改其他方法声明。

谢谢。

关于swift - config.preparations.append(modelName.self) 错误使用未解析的标识符 'modelName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46429079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com