字符串中的空格导致致命错误:无法超出endIndex

时间:2016-11-07 17:44:16

标签: string macos substring swift3

我有一个字符串" PIXEL STUDIOS - TEST1"我的代码一直有效,直到我到达字符串中的第一个空格。

for j = 1:4
    n = (co(e(j,2),:) - co(e(j,1),:));
    n = n./norm(n);
    F = e(j,5).*e(j,4)./e(j,3).*-(dot(u,n)).*n
    theta = acosd(dot(F,n)/(norm(F)*norm(n)))

    if theta == 180
        F2 = [F2; -sqrt(F(1)^2 + F(2)^2 + F(3)^2)]
    else if theta == 0
            F2 = [F2; sqrt(F(1)^2 + F(2)^2 + F(3)^2)]
        end
    end
end

当我运行我的代码和offsetBy 5时,它可以工作,但是当我尝试偏移超过该点时,我得到了错误。我需要做些什么来处理字符串中的空格吗?

1 个答案:

答案 0 :(得分:0)

您的代码正在使用给定的字符串:

var str =  "PIXEL STUDIOS - TEST1"
let c = str.characters
let r = c.index(c.startIndex, offsetBy: 6)..<c.index(c.endIndex, offsetBy: 0)
let substring = str[r]
print(substring)

打印:

STUDIOS - TEST1

结论:label.stringValue很可疑。

打印出来进行进一步调查。