Swift 5.1 子字符串问题
Swift 5.1 Substring Issue
我是 Swift 的新手。我用 Swift 5.1
我想从一个字符串中获取一个子字符串,但我做不到。
我已经尝试了几种解决方案(, ),但这些都不适合我。
我这样试过:
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x);
let end = s.index(s.startIndex, offsetBy: y);
return s[start..<end];
}
print(substring(x: 0, y: 2, s: "abcde"))
/tmp/306AE87E-57C7-417C-B2EF-313A921E75B9.BuUdsc/main.swift:6:11:
error: subscript 'subscript(_:)' requires the types 'String.Index' and
'Int' be equivalent return s[start..(bounds: R) -> String where R : RangeExpression, R.Bound == Int { get }
^
非常感谢您的帮助。谢谢。
您的代码大部分没问题,但 Swift 弄乱了错误消息。我在 Xcode 11.4 beta 中用 Swift 5.2 更好的错误诊断尝试了你的代码,它抱怨 return 类型 String
不正确,因为 s[start..<end]
给你一个Substring
。您可以更改 return 类型:
func substring(x : Int, y : Int, s : String) -> Substring {
let start = s.index(s.startIndex, offsetBy: x)
let end = s.index(s.startIndex, offsetBy: y)
return s[start..<end]
}
print(substring(x: 0, y: 2, s: "abcde"))
或将子字符串转换为字符串:
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x)
let end = s.index(s.startIndex, offsetBy: y)
return String(s[start..<end])
}
print(substring(x: 0, y: 2, s: "abcde"))
旁注:Swift 中不需要 ;
,除非您想在一行中包含多个语句,否则不使用它是一种惯例。
作为您的函数 returns 一个字符串,您需要将 s[start..<end]
即 Substring
转换为 String
。
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x);
let end = s.index(s.startIndex, offsetBy: y);
return String(s[start..<end])
}
print(substring(x: 0, y: 2, s: "abcdefghijklmnopqrstuvwxyz"))
输出:
ab
这又是那些令人困惑的错误消息之一,这些消息并没有告诉您希望您确实做错了。
你应该这样做:
return String(s[start..<end])
这是因为取一个Range<String.Index>
的下标其实是returns一个Substring
,但是你的方法returns一个String
,所以你得返回前转换它。
关于为什么会输出错误信息的推测:
看到方法 returns a String
,Swift 编译器试图找到一个 returns a String
的下标,以及它如何找到了一个(我找不到),但该重载仅适用于 Index
关联类型为 Int
.
的类型
我是 Swift 的新手。我用 Swift 5.1
我想从一个字符串中获取一个子字符串,但我做不到。
我已经尝试了几种解决方案(
我这样试过:
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x);
let end = s.index(s.startIndex, offsetBy: y);
return s[start..<end];
}
print(substring(x: 0, y: 2, s: "abcde"))
/tmp/306AE87E-57C7-417C-B2EF-313A921E75B9.BuUdsc/main.swift:6:11: error: subscript 'subscript(_:)' requires the types 'String.Index' and 'Int' be equivalent return s[start..(bounds: R) -> String where R : RangeExpression, R.Bound == Int { get } ^
非常感谢您的帮助。谢谢。
您的代码大部分没问题,但 Swift 弄乱了错误消息。我在 Xcode 11.4 beta 中用 Swift 5.2 更好的错误诊断尝试了你的代码,它抱怨 return 类型 String
不正确,因为 s[start..<end]
给你一个Substring
。您可以更改 return 类型:
func substring(x : Int, y : Int, s : String) -> Substring {
let start = s.index(s.startIndex, offsetBy: x)
let end = s.index(s.startIndex, offsetBy: y)
return s[start..<end]
}
print(substring(x: 0, y: 2, s: "abcde"))
或将子字符串转换为字符串:
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x)
let end = s.index(s.startIndex, offsetBy: y)
return String(s[start..<end])
}
print(substring(x: 0, y: 2, s: "abcde"))
旁注:Swift 中不需要 ;
,除非您想在一行中包含多个语句,否则不使用它是一种惯例。
作为您的函数 returns 一个字符串,您需要将 s[start..<end]
即 Substring
转换为 String
。
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x);
let end = s.index(s.startIndex, offsetBy: y);
return String(s[start..<end])
}
print(substring(x: 0, y: 2, s: "abcdefghijklmnopqrstuvwxyz"))
输出:
ab
这又是那些令人困惑的错误消息之一,这些消息并没有告诉您希望您确实做错了。
你应该这样做:
return String(s[start..<end])
这是因为取一个Range<String.Index>
的下标其实是returns一个Substring
,但是你的方法returns一个String
,所以你得返回前转换它。
关于为什么会输出错误信息的推测:
看到方法 returns a String
,Swift 编译器试图找到一个 returns a String
的下标,以及它如何找到了一个(我找不到),但该重载仅适用于 Index
关联类型为 Int
.