我试图做一件简单的事情 - 提取字符串的硬编码部分。例如来自" 123"我需要来自" 0123" - " 01"。 现在我刚刚阅读了所有关于[从我的观点来看非常奇怪]的教程中的Swift中的Range实现,但仍然无法使这个简单的代码按照我的要求运行:
let start2 = string.startIndex
let end2 = string.index(string.endIndex, offsetBy: -3)
minutes = Int(string.substring(with: start2..<end2))
问题在于&#34; 123&#34;在这种情况下,start2等于end2,substring返回nil。有任何想法吗?
P.S。伟大的上帝,即使在20年前的汇编程序中,这项操作也是微不足道的
答案 0 :(得分:0)
你想删掉最后两个字符。
有多种解决方案。一个是
#if DEBUG
if (state != "Ok")
{
Debug.WriteLine($"Error occured: {state}, {moreInfo}");
}
#endif
或
let string = "0123"
let minutes = Int(String(string.characters.dropLast(2)))!
既然你想要let range = string.index(string.endIndex, offsetBy: -2)..<string.endIndex
let minutes = Int(string.substring(to: range.lowerBound))!
,为什么不简单
Int