gpt4 book ai didi

swift - 无法使用类型 'Int' 的参数列表调用类型 '(Number)' 的初始值设定项

转载 作者:行者123 更新时间:2023-11-30 12:25:52 28 4
gpt4 key购买 nike

该程序无法通过 Xcode 编译,只能在 IOS 应用程序“Playgrounds”中运行。

在 IOS 应用程序“Playgrounds”->“Learn to Code 3”(Swift 3.1) ->“Music Universe”中,我有以下代码:

// A touch event for when your finger is moving across the scene.

// Declaration
struct Touch

// The position of this touch on the scene.

// Declaration
var position: Point

// The x coordinate for the point.

// Declaration for touch.position.x
var x: Double

以上仅供说明。

let touch: Touch
let numberOfNotes = 16
let normalizedXPosition = (touch.position.x + 500) / 1000
let note = normalizedXPosition * (numberOfNotes - 1)
let index = Int(note)

最后一句显示错误:无法使用“(Number)”类型的参数列表调用“Int”类型的初始值设定项。如何将 note 转换为 Int 类型?

最佳答案

它在 iPad 应用程序 Swift Playgrounds 上运行。

他们显然在幕后创建了一个协议(protocol) Number来减轻 Swift 类型转换的痛苦。在此小程序中,可以将 IntDouble 相乘,而无需先将 Int 转换为 Double >:

let a = 5      // a is an Int
let b = 6.3 // b is a Double
let c = a * b // This results in c being type Number

Number 具有只读属性 intdouble,它们返回 IntDouble 数字的表示形式。

var int: Int { get }
var double: Double { get }

因此,如果您需要 indexInt,请这样做:

let index = note.int

关于swift - 无法使用类型 'Int' 的参数列表调用类型 '(Number)' 的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224813/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com