无法将类型“ [_]”的值转换为指定的类型“数组”

扎克·罗森塔尔(Zuck Rossental)

错误信息:

无法将类型“ [_]”的值转换为指定类型“数组”

错误行:

var frontier: Array = []
var finalPaths: Array = []

码:

import UIKit

public class Vertex {
    var key: String?
    var neighbors: Array<Edge>
    init() {
        self.neighbors = Array<Edge>()
    }
}

public class Edge {
    var neighbor: Vertex
    var weight: Int
    init() {
        weight = 0
        self.neighbor = Vertex()
    }
}

class Path {
    var total: Int!
    var destination: Vertex
    var previous: Path!

    //object initialization
    init(){ destination = Vertex() }
}

public class SwiftGraph {
    private var canvas: Array<Vertex>
    public var isDirected: Bool
    init() {
        canvas = Array<Vertex>()
        isDirected = true
    }
    //create a new vertex
    func addVertex(key: String) -> Vertex {
        //set the key
        var childVertex: Vertex = Vertex()
        childVertex.key = key
        //add the vertex to the graph canvas
        canvas.append(childVertex)
        return childVertex
    }
    func addEdge(source: Vertex, neighbor: Vertex, weight: Int) {
        //create a new edge
        var newEdge = Edge()
        //establish the default properties
        newEdge.neighbor = neighbor
        newEdge.weight = weight
        source.neighbors.append(newEdge)
        //check for undirected graph
        if (isDirected == false) {
            //create a new reversed edge
            var reverseEdge = Edge()
            //establish the reversed properties
            reverseEdge.neighbor = source
            reverseEdge.weight = weight
            neighbor.neighbors.append(reverseEdge)
        }
    }
    func processDijkstra(source: Vertex, destination: Vertex) -> Path? {
        var frontier: Array = []
        var finalPaths: Array = []
        //use source edges to create the frontier
        for e in source.neighbors {
            var newPath: Path = Path()
            newPath.destination = e.neighbor
            newPath.previous = nil
            newPath.total = e.weight
            //add the new path to the frontier
            frontier.append(newPath)
        }

        //obtain the best path
        var bestPath: Path = Path()
        while(frontier.count != 0) {
            //support path changes using the greedy approach
            bestPath = Path()
            var x: Int = 0
            var pathIndex: Int = 0
            for (x = 0; x < frontier.count; x++) {
                var itemPath: Path = frontier[x] as Path
                if (bestPath.total == nil) || (itemPath.total < bestPath.total) {
                    bestPath = itemPath
                    pathIndex = x
                }
            }

            for e in bestPath.destination.neighbors {
                var newPath: Path = Path()
                newPath.destination = e.neighbor
                newPath.previous = bestPath
                newPath.total = bestPath.total + e.weight
                //add the new path to the frontier
                frontier.append(newPath)
            }
            //preserve the bestPath
            finalPaths.append(bestPath)
            //remove the bestPath from the frontier
            frontier.removeAtIndex(pathIndex)
        }
        printSeperator("FINALPATHS")
        printPaths(finalPaths as [Path], source: source)
        printSeperator("BESTPATH BEFORE")
        printPath(bestPath, source: source)
        for p in finalPaths {
            var path = p as Path
            if (path.total < bestPath.total) && (path.destination.key == destination.key){
                bestPath = path
            }
        }
        printSeperator("BESTPATH AFTER")
        printPath(bestPath, source: source)
        return bestPath
    }

    func printPath(path: Path, source: Vertex) {
        print("BP: weight- \(path.total) \(path.destination.key!) ")
        if path.previous != nil {
            printPath(path.previous!, source: source)
        } else {
            print("Source : \(source.key!)")
        }
    }

    func printPaths(paths: [Path], source: Vertex) {
        for path in paths {
            printPath(path, source: source)
        }
    }

    func printLine() {
        print("*******************************")
    }

    func printSeperator(content: String) {
        printLine()
        print(content)
        printLine()
    }
}



var graph = SwiftGraph()
var a = graph.addVertex("a")
var a1 = graph.addVertex("a1")
var b = graph.addVertex("b")
var c = graph.addVertex("c")
var d = graph.addVertex("d")
graph.addEdge(a, neighbor: d, weight: 1)
graph.addEdge(a, neighbor: d, weight: 30)
graph.addEdge(a, neighbor: a1, weight: 30)
graph.addEdge(a1, neighbor: b, weight: 10)
graph.addEdge(a, neighbor: b, weight: 5)
graph.addEdge(b, neighbor: c, weight: 21)
graph.addEdge(c, neighbor: d, weight: 5)

var path = graph.processDijkstra(a, destination: d)
埃里克·艾雅

您必须为数组指定正确的类型。

由于它们将包含Path对象,因此请这样声明:

var frontier: [Path] = []
var finalPaths: [Path]  = []

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将类型'()-> _'的值转换为指定的类型UIImageView

无法将“数据”类型的值转换为指定的“数据”类型

无法将“() -> _”类型的值转换为指定类型“[UIViewController]”

无法将IEnumerable转换为指定的类型

Swift属性yWrapper无法将声明类型的值转换为指定类型的值

Swift错误:无法将类型'()'转换为指定类型'布尔'

OrderedDictionary 无法将类型“[String : Double]”的值转换为指定类型“OrderedDictionary<Key, Value>”

准备segue:无法将类型“ UIViewController”的值转换为指定的类型“ SecondViewController”

Swift 2.2:无法将类型“ [B]”的值转换为指定类型“ [A]”

无法将类型“ NSFetchRequest <NSFetchRequestResult>”的值转换为指定的类型“ NSFetchRequest <T>”

Swift:无法将类型“ [Any]”的值转换为指定的类型“ NSString”

无法将类型 (Product) -> [String :Any ] 的值转换为指定类型 [ String : Any]

Swift 错误:无法将类型 '() throws -> ()' 的值转换为指定类型 'Bool'

无法将“字符串”类型的值转换为指定类型“NWEndpoint.Host”

Swift:无法将“Range<Int>”类型的值转换为指定类型“Int”

无法将类型“ [T]”的值转换为预期的参数类型“ [_]”

无法将类型“ A <T>”的值转换为预期的参数类型“ A <_>”

无法将 '(ViewController) -> () -> ()' 类型的值转换为预期的参数类型 '() -> ()'

无法将类型的值转换为预期的参数类型NSURLSessionDelegate

无法将类型的值转换为预期的参数类型“布尔”

无法将类型'()'的值转换为预期的参数类型'()->()'

无法将类型“ Int”的值转换为预期的参数类型“ _?”

无法将“()”类型的值转换为预期的元素类型“NSLayoutConstraint”

无法强制将“ [记录]”类型的值转换为“记录”类型

无法将类型“ Int”的值转换为原始类型“ DarwinBoolean”

无法将类型'(_)->()'的值转换为预期的参数类型'(()-> Void)?

无法将“()”类型的值转换为预期的参数类型“(() -> Void)?”

无法强制将'UIViewController'类型的值转换为'TableView'类型

无法将类型[()]的值转换为预期的参数类型()