Swift3 Swift 3转换处理多个问题
我已经开始转换到swift 3,同时尽可能多地删除NS类,但他的代码遇到了障碍:Swift3 Swift 3转换处理多个问题,swift3,Swift3,我已经开始转换到swift 3,同时尽可能多地删除NS类,但他的代码遇到了障碍: var S: String = ADataItem.description_text; // FRegExBufui_Image is of type NSRegularExpression let matches: [NSTextCheckingResult] = FRegexBufUI_Image.matches(in: S, options: NSRegularExpression.MatchingOptio
var S: String = ADataItem.description_text;
// FRegExBufui_Image is of type NSRegularExpression
let matches: [NSTextCheckingResult] = FRegexBufUI_Image.matches(in: S, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: S.characters.count));
if matches.count > 0 {
for m in 0 ..< matches.count {
S = S.substring(with: match.rangeAt(m));
var S:String=ADataItem.description\u text;
//FRegExBufui_图像的类型为NSRegularExpression
让matches:[NSTextCheckingResult]=FRegexBufUI_Image.matches(在:S中,选项:NSRegularExpression.MatchingOptions(),范围:NSRange(位置:0,长度:S.characters.count));
如果匹配。计数>0{
对于0中的m..
我犯了一个错误
无法将“NSRange”(又名“\u NSRange”)类型的值转换为预期值
参数类型“范围”
(又名“范围”)
我想问题的原因可能是我现在将swift数据类型/类与NS混合在一起
这里的mos clean解决方案…就是简单地将NSRange转换为Range?或者,当我需要使用正则表达式时,是否有一种方法可以完全快速执行?Swift
Range
和NSRange
是不同的东西。看起来函数需要一个可以使用创建的快速范围。SwiftRange
和NSRange
是不同的东西。看起来函数需要一个可以使用创建的快速范围。最简单的方法是将字符串连接到NSString
let matches = FRegexBufUI_Image.matches(in: S, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: S.characters.count));
for match in matches { // don't use ugly C-style index based loops
let substring = (S as NSString).substring(with: match.rangeAt(m))
}
如果不想使用混合类型,请实现此字符串
扩展,它将范围
转换为NSRange
:
extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self)
else { return nil }
return from ..< to
}
func substring(withNSRange range : NSRange) -> String
{
let swiftRange = self.range(from : range)
return swiftRange != nil ? self.substring(with: swiftRange!) : self
}
}
编辑:
在Swift 4+中,扩展名已过时。有一个方便的初始值设定项可以从NSRange
for match in matches { // don't use ugly C-style index based loops
let stringRange = Range(match.range(at: m), in: S)!
let substring = String(S[stringRange])
}
最简单的方法是将字符串连接到NSString
let matches = FRegexBufUI_Image.matches(in: S, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: S.characters.count));
for match in matches { // don't use ugly C-style index based loops
let substring = (S as NSString).substring(with: match.rangeAt(m))
}
如果不想使用混合类型,请实现此字符串
扩展,它将范围
转换为NSRange
:
extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self)
else { return nil }
return from ..< to
}
func substring(withNSRange range : NSRange) -> String
{
let swiftRange = self.range(from : range)
return swiftRange != nil ? self.substring(with: swiftRange!) : self
}
}
编辑:
在Swift 4+中,扩展名已过时。有一个方便的初始值设定项可以从NSRange
for match in matches { // don't use ugly C-style index based loops
let stringRange = Range(match.range(at: m), in: S)!
let substring = String(S[stringRange])
}
问题是我需要从正则表达式中使用match.rangeAt(m)。我可以转换它的结果吗,这样它就可以在S.substring中?问题是我需要从正则表达式中使用match.rangeAt(m)。我可以转换它的结果吗,这样它就可以在S.substring中?