2017-09-26 80 views
0

我已經通過流暢提供商的正確方法去建立一個關係,我模型(SWIFT)服務器端)PostgreSQL的提供商(數據庫),我遵循流利的一般方法,但我不知道我做的錯誤,在擴展MODELNAME準備, 我modelName.swift的代碼如下和main.swift錯誤config.preparations.append使用</strong><strong>蒸汽((modelName.self)使用未解決的標識符「MODELNAME」

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) 
    } 
} 

main.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() 

回答

1

我認爲根本原因是模塊是分開的。 如果您創建的蒸氣項目爲vapor new,main.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 { 
... 

請注意,您還必須根據此更改修改其他方法聲明。

謝謝。

+0

我改成了公衆,但仍然出現同樣的錯誤,目錄,main.swift處於運行模塊和modelName.swift是在應用程序模塊,使內部的模型文件夾。運行模塊默認在App模塊中。無法解決這個錯誤呢, –

0

移動modelName.swift運行文件夾

相關問題