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

2023-12-24

错误信息:

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

错误行:

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

Code:

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]  = []
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将类型“[_]”的值转换为指定类型“Array” 的相关文章

  • 正在解除分配时尝试加载视图控制器的视图... UISearchController

    我有创建一个的代码UISearchController in my UIVIew sviewDidLoad self resultSearchController let controller UISearchController sear
  • “无法调用非函数类型‘HTTPURLResponse’的值?”(Alamofire 4.0) [Swift 3.0]

    我收到此错误 无法调用非函数类型 HTTPURLResponse 的值 关于该部分 response request response data error 我想知道是否有人可以帮助我解决这个问题 Alamofire download ur
  • SwiftUI NavigationView 看不到图像

    我有一个代码并制作 NavigationLink 按钮 我编写文本和图像 但我的图像看不到 请帮助我 VStack Image Coachs resizable aspectRatio contentMode fill frame widt
  • 执行数组内的函数

    是否可以从数组内部执行匿名函数 也在数组内部定义 return execute function logic 或者我应该在外面定义它然后再调用它 从技术上讲 您可以将该函数括在括号中并像这样调用它 return function retur
  • 在php中对带有特殊字符的多维数组进行排序

    我有一个多维数组 我按字母顺序排序 但问题是 带有丹麦语特殊字符 它们应该按该顺序排序 但不会按该顺序返回 这是我的数组 部分已删除 Array 0 gt Array Name gt John 1 gt Array Name gt Pate
  • 编译器消息“警告:格式‘%s’需要类型‘char *’,但参数 2 具有类型‘char (*)’”

    我正在尝试运行一个简单的 C 程序 但收到此错误 警告 格式 s 需要类型 char 但参数 2 的类型为 char 20 我在跑步Mac OS X v10 8 https en wikipedia org wiki OS X Mounta
  • 在 Swift 3 中单击和双击 UITableViewCell

    我在 TableView Cell 上有故事板 segue 我用它来在单元格单击中传输到另一个 VCdidSelectRowAt方法 现在我双击了TapGestureRecognizer处理手机上的点击问题 问题是 单击时 segue 正在
  • 根据数组计数填充复选框

    我是新来的php我已经使用了它的开发php array 我想填充checkboxes根据数组计数 为了做到这一点 我尝试了以下方式 它对我不起作用 有什么方法可以做到这一点 在我的例子中 数组计数 5 所以我相应地需要 5 个复选框
  • 默认数组值

    有没有办法在javascript中为数组分配默认值 ex an array with 24 slots that defaults to 0 您可以使用fill数组上的函数 Array 24 fill 0 Note fill仅在 ECMAS
  • 在 postgresql 9.4 或 9.5 中查询 json 对象的嵌套数组中的元素

    studentID 1 StudentName jhon Data schoolname school1 enrolmentInfo year 2015 info courseID csc213 school IT enrollmentda
  • Swift 对异步编程有什么语言级别的支持(如果有)?

    当应用程序必须通过不可预测的网络 例如智能手机应用程序 进行通信时 异步编程对于响应式用户界面来说是必须的 用户界面必须保持响应 同时等待结果从互联网上某处的服务器返回 在大多数语言中 应用程序程序员必须实现自己的状态机 可能使用闭包 来响
  • iOS 内存警告

    我正在尝试使用从 Parse 数据库下载的图像填充集合视图 但我收到内存警告 然后偶尔崩溃 有谁知道其他应用程序如何设法呈现这么多图像而不崩溃 有人可以告诉我如何优化我已有的东西吗 这是所有相关代码 https gist github co
  • 数组初始化编译时间 - Constexpr 序列

    我正在读书this https stackoverflow com questions 45938798 for loop with variable step size c 45939130关于SO的问题 这个问题本身并不是那么有趣 但我
  • 使用 OpenCV 描述符与 findFundamentalMat 匹配

    我之前发布了有关同一程序的问题 但没有收到答案 我已经纠正了当时遇到的问题 但又面临新的问题 基本上 我使用未校准的方法自动校正立体图像对的旋转和平移 我使用 SURF 等特征检测算法来查找两个图像 左右立体图像对 中的点 然后再次使用 S
  • 如何 grep 遍历数组,同时过滤掉匹配项?

    有没有一种快速简便的方法来 grep 遍历数组 找到满足某些测试的元素and从原始数组中删除这些 例如我想要 a 1 7 6 3 8 4 b grep filter gt 5 a now b 7 6 8 and a 1 3 4 换句话说 我
  • 如何自动为 Swift 类创建初始化程序?

    UPDATE 使用结构而不是类 struct 在很多方面都更好 它有自己的初始化器 这是我的模型课 是否有可能创建init自动方法 每次我都必须将所有变量一一初始化 这会花费很多时间 class Profile var id String
  • 将一维数组转换为二维数组[重复]

    这个问题在这里已经有答案了 我正在开发一个程序 我必须将文本文件中的值读入一维数组 我已经成功获取该一维数组中的数字 m1 1 2 3 4 5 6 7 8 9 但我希望数组是 m1 1 2 3 4 5 6 7 8 9 您可以使用此代码 co
  • 下标:使用字符串枚举访问我的字典值

    我想做类似的事情 使用字符串枚举访问我的字典值 我试图重载字典的下标但没有成功 访问字典 let district address JsonKeys district 其中 JsonKeys 是 enum JsonKeys String c
  • 当设置 setVisibleXRangeMaximum 时,iOS-Charts X 轴值无限重复

    我正在尝试绘制一个图表 其中 x 轴是TimeIntervalY 轴是power consumption 由于每天都会有数据 因此将有太多数据无法显示 因此 我想一次显示 5 个值 我通过设置实现了这一点self chart setVisi
  • 通用类不会将委托调用转发给具体子类

    鉴于以下情况 protocol EntityType var displayString String get extension String EntityType var displayString String return self

随机推荐