2017-05-28 80 views
0

此程序无法通过Xcode进行编译,只能在IOS应用程序“Playgrounds”中运行。无法使用类型为'(Number)'的参数列表调用类型为'Int'的初始值设定项。

在IOS应用 “游乐场” - > “学习代码3”(SWIFT 3.1) - > “音乐宇宙” 我有下面的代码:

// 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) 

最后一句显示错误: 不能调用初始化类型“诠释”类型的参数列表“(编号)”。 我怎么能转换noteInt类型?

+1

什么类型都有'touch.position.x'? - 一个显示问题的自包含*示例将有所帮助。 –

+0

对于第一次看,我认为'touch.position.x'类型为CGFloat的,但现在看来,这是不是因为你应该得到'让注= normalizedXPosition *(numberOfNotes - 1)编译时错误',抱怨关于Int和CGFloat之间的乘积。 –

+0

我已添加touch.position.x的注释,touch.position.x类型为'Double' – linjie

回答

1

这是在iPad上运行Swift Playgrounds

他们显然在幕后创建了 Number以缓解Swift类型转换的痛苦。在这种小型的程序,它是能够繁殖的IntDouble而不首先转换IntDouble

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 } 

所以,如果你需要index是一个Int,像这样做:

let index = note.int 
相关问题