无法将类型“ [_]”的值转换为指定的类型“数组” [英] Cannot convert value of type '[_]' to specified type 'Array'

查看:61
本文介绍了无法将类型“ [_]”的值转换为指定的类型“数组”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误信息:


无法将类型'[_]'的值转换为指定类型'Array'

cannot convert value of type '[_]' to specified type 'Array'

错误行:

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)


推荐答案

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

You have to give the right type to your arrays.

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

Since they will contain Path objects, declare like this:

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

这篇关于无法将类型“ [_]”的值转换为指定的类型“数组”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆