问题描述
我已经分离出我的项目,可以粘贴到一个游乐场一些斯威夫特code。它会产生一个错误的找不到'+'接受提供的参数过载的无论是在普通的X code编辑和游乐场。该错误指的是最后一个(非平凡)线。
I've isolated some Swift code from my project that can be pasted into a Playground. It produces an error "Could not find an overload for '+' that accepts the supplied arguments" both in normal Xcode editing and the Playground. The error refers to the last (non-trivial) line.
import UIKit
let points = 40
let max = points-1
let L = 10.0
let Deltat = 0.01
let Deltax = L/Double(points)
var a = [Double](count: points, repeatedValue: 0.0)
var b = [Double](count: points, repeatedValue: 0.0)
var c = [Double](count: points, repeatedValue: 0.0)
for i in 1..<max-1
{ //let iPlus1 = i+1
//let temp = 0.5*Deltat/Deltax
c[i] = 0.5*(a[i+1] + a[i-1]) + 0.5*Deltat/Deltax * (b[i+1] - b[i-1])
}
如果我取消注释行让iPlus1 ......并进行以下修改,斯威夫特接受code。
If I uncomment the line "let iPlus1..." and make the following edit, Swift accepts the code.
{ let iPlus1 = i+1
//let temp = 0.5*Deltat/Deltax
c[i] = 0.5*(a[iPlus1] + a[i-1]) + 0.5*Deltat/Deltax * (b[i+1] - b[i-1])
}
如果我取消注释行让温度......并进行以下修改,斯威夫特再次接受code。
If I uncomment the line "let temp..." and make the following edit, Swift again accepts the code.
{ //let iPlus1=i+1
let temp = 0.5*Deltat/Deltax
c[i] = 0.5*(a[i+1] + a[i-1]) + temp * (b[i+1] - b[i-1])
}
无论这些编辑的任何意义对我来说,因为都是看似微不足道替换。我知道,斯威夫特绝对不会隐式类型转换我。似乎没有要试图在原来的code任何隐含的类型转换 - 所有类型为int和双打都如预期宣布。我开始相信这是与雨燕数组下标索引的错误。
Neither of these edits make any sense to me, as both are seemingly trivial substitutions. I am aware that Swift will never implicitly typecast for me. There doesn't seem to be any implicit typecasting attempted in the original code -- all Ints and Doubles are declared as intended. I'm starting to believe this is a bug with the Swift array subscript indexing.
推荐答案
这是一个已知的bug迅速:长语句产生奇怪的编译错误。
只是分割你的线,2号线,如:
This is a known swift bug: long statements generate strange compilation errors.Just split your line in 2 lines, such as:
c[i] = 0.5*(a[i+1] + a[i-1])
c[i] += 0.5*Deltat/Deltax * (b[i+1] - b[i-1])
我发现,发生在同一行超过4个或5个算术运算,但是这不是一个规则,只是一些前pression类型发现了一些 - 它可能是在其他情况下,不同的
I found out that happens with more than 4 or 5 arithmetic operations in the same line, but that's not a rule, just a number found with some expression types - it may be different in other cases.
查找例如在这个Q&安培; A:和(这最后一个实际上导致缓慢,但解决了以同样的方式,因此,根可能是相同的)
Look for example at this Q&A: Xcode Beta 6.1 and Xcode 6 GM stuck indexing for weird reason and Xcode 6 with Swift super slow typing and autocompletion (this last one actually causes slowness, but solved in the same way, so the root is probably the same)
这篇关于错误斯威夫特数组下标索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!