代码之家  ›  专栏  ›  技术社区  ›  Chewie The Chorkie

swift:无法转换“int”类型的值。到指定类型“uint32”

  •  2
  • Chewie The Chorkie  · 技术社区  · 7 年前

    我试图给uint32分配一个数组计数。我得到错误“无法转换'int'类型的值?”到指定的类型“uint32”。数组计数是int类型,但错误是“int?”,它看起来像一个可选的int。我不知道它还有什么含义。

    let randColorCount:UInt32 = slider?.randUIColors.count
    

    我也试过:

    let randColorCount:UInt32 = UInt32(slider?.randUIColors.count)
    

    但是我得到一个错误“cannot invoke initializer for type'uint32'with a argument list of type'(int?)”是的。

    1 回复  |  直到 7 年前
        1
  •  6
  •   rmaddy    7 年前

    slider?.randUIColors.count 结果 Int? 因为使用了可选的链接。

    一个简单的解决方案是将其与nil凝聚算子相结合 ?? 提供默认值 slider nil 是的。

    let randColorCount = UInt32(slider?.randUIColors.count ?? 0)
    
    推荐文章