【问题标题】:Swift 2.0: NSRangeException, Range or index out of boundsSwift 2.0:NSRangeException、范围或索引超出范围
【发布时间】:2015-09-11 15:03:20
【问题描述】:

背景:我来自 15-20 年的 JavaScript、Ruby 和(现代)PHP。去年我一直在研究 Swift,而我对 Cocoa 是全新的。

这是我在 Xcode 7.0 β2 中运行的简化测试用例:

#! /usr/bin/env swift

import Foundation

// Extend the String object with helpers
extension String {

    // String.replace(); similar to JavaScript's String.replace() and Ruby's String.gsub()
    func replace(pattern: String, replacement: String) -> String {

        // Debugging
        print(self.characters.count)
        print(NSMakeRange(0, self.characters.count - 1))

        let regex = try! NSRegularExpression(
            pattern: pattern,
            options: [.CaseInsensitive]
        )

        return regex.stringByReplacingMatchesInString(
            replacement,
            options: [.Anchored],
            range: NSMakeRange(0, self.characters.count - 1),
            withTemplate: "xx"
        )
    }
}

let prefix = "abc     123".replace("\\s+", replacement: " ")

print(prefix)

两条调试线显示:

11
(0,10)

之后,应用程序崩溃并显示以下消息:

2015-06-24 23:18:45.027 swift[42912:648900] *** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:] : 范围或索引超出范围'

我查看了以下文档,但没有任何内容:

我只能认为该问题与将 NSRange 实例作为参数传递给 stringByReplacingMatchesInString() 方法有关,但我已尝试将值调整为 NSRange(0,1)NSRange(1,2) 期望看到一些东西会有所帮助,但它仍然抛出异常。

正如我在标题中所写,我正在使用 Swift 2.0

【问题讨论】:

  • 我不知道这是否是您的问题的原因,但请看这里关于使用 NSRage nshipster.com/nsrange

标签: swift nsregularexpression swift2 nsrangeexception


【解决方案1】:

我看到 stringByReplacingMatchesInString 的第一个参数是“替换”。这应该是“自我”吗?

【讨论】:

  • facepalm。你是对的。但是接下来 - replacement 值去哪里了?我有我编译的正则表达式,我在上面调用stringByReplacingMatchesInString(),传递self
【解决方案2】:

在@Ben 的回答之后,我觉得自己很愚蠢。这开始了更多的探索,我想通了。这是代码。

#! /usr/bin/env swift

import Foundation

// Extend the String object with helpers
extension String {

    // String.replace(); similar to JavaScript's String.replace() and Ruby's String.gsub()
    func replace(pattern: String, replacement: String) -> String {

        let regex = try! NSRegularExpression(
            pattern: pattern,
            options: [.CaseInsensitive]
        )

        return regex.stringByReplacingMatchesInString(
            self,
            options: [.WithTransparentBounds],
            range: NSMakeRange(0, self.characters.count),
            withTemplate: replacement
        )
    }
}

let prefix = "abc     123".replace("(\\s+)", replacement: " ")

print(prefix)

【讨论】:

    【解决方案3】:

    也许:

       Return regex.stringByReplacingMatchesInString{
            MainstringYouWantToReplaceSomethinIn,
            options: [.Anchored],
            range: NSMakeRange(0,self.MainstringYouWantToReplaceSomethinIn.count - 1),
            withTemplate: "xx"
        )
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多