jjzjj

swift - 使 String.CharacterView.Index 符合 Strideable : fatal error when using stride(to:by:): "cannot increment endIndex "

coder 2023-09-06 原文

问题:

当尝试通过例如跨越 String.CharacterView.Index 索引时2 的一大步

extension String.CharacterView.Index : Strideable { }

let str = "01234"
for _ in str.startIndex.stride(to: str.endIndex, by: 2) { } // fatal error

我收到以下运行时异常

fatal error: cannot increment endIndex



但是,仅在创建上面的 StrideTo<String.CharacterView.Index> 时,( let foo = str.startIndex.stride(to: str.endIndex, by: 2) )不会产生错误,仅在尝试跨步/迭代或对其进行操作时( .next() ?)。
  • 这个运行时异常是什么原因;这是预期的(误用符合 Stridable )?

  • 我正在使用 Swift 2.2 Xcode 7.3 。详情如下。

    编辑补充:错误源位于

    仔细阅读我的问题后,似乎错误确实发生在 next()StrideToGenerator 方法中(参见本文底部),特别是在以下标记行
    let ret = current
    current += stride  // <-- here
    return ret
    

    即使永远不会返回 current 的最后一次更新(在下一次调用 next() 中),current 索引的最终推进值大于或等于 _end 的值会产生上述特定的运行时错误(对于 Index 类型 String.CharacterView.Index )。
    (0..<4).startIndex.advancedBy(4) // OK, -> 4
    "foo".startIndex.advancedBy(4)   // fatal error: cannot increment endIndex
    

    但是,仍然存在一个问题:
  • 这是 next()StrideToGenerator 方法中的错误,还是只是由于误用 String.CharacterView.IndexStridable 的一致性而弹出的错误?


  • 有关的

    以下问答与在 +1 以外的步骤中迭代字符的主题有关,即使两个问题不同,也值得包含在此问题中。
  • Using String.CharacterView.Index.successor() in for statements

  • 特别注意上面线程中的 @Sulthan:s neat solution

    细节

    (对于我自己的大量细节/调查表示歉意,如果您可以在没有此处详细信息的情况下回答我的问题,请跳过这些部分)

    String.CharacterView.Index type 描述了一个字符位置,并且:
  • 符合 Comparable (因此, Equatable ),
  • 包含 advancedBy(_:)distanceTo(_:) 的实现。

  • 因此,它可以直接符合 protocol Strideable ,利用 Stridable :s 方法 stride(through:by:)stride(to:by:) 的默认实现。下面的例子将侧重于后者(与前者类似的问题):

    ...

    func stride(to end: Self, by stride: Self.Stride) -> StrideTo<Self>
    

    Returns the sequence of values (self, self + stride, self + stride + stride, ... last) where last is the last value in the progression that is less than end.



    符合 Stridable 并通过 1 大步前进:一切都很好

    String.CharacterView.Index 扩展到 Stridable 并通过 1 跨步工作正常:
    extension String.CharacterView.Index : Strideable { }
    
    var str = "0123"
    
    // stride by 1: all good
    str.startIndex.stride(to: str.endIndex, by: 1).forEach {
        print($0,str.characters[$0])
    } /* 0 0 
         1 1 
         2 2
         3 3 */
    

    对于上面 str 中的偶数个索引(索引 0..<4 ),这也适用于 2 的步幅:
    // stride by 2: OK for even number of characters in str.
    str.startIndex.stride(to: str.endIndex, by: 2).forEach {
        print($0,str.characters[$0])
    } /* 0 0
         2 2 */
    

    但是,对于某些通过 >1 跨步的情况:运行时异常

    但是,对于奇数个索引和 2 的步幅,字符 View 索引上的步幅会产生运行时错误
    // stride by 2: fatal error for odd number of characters in str.
    str = "01234"
    str.startIndex.stride(to: str.endIndex, by: 2).forEach {
        print($0,str.characters[$0])
    } /* 0 0
         2 2
         fatal error: cannot increment endIndex */
    

    我自己的调查

    我自己对此的调查使我怀疑错误来自 next() structureStrideToGenerator 方法,可能是当此方法在 stridable 元素上调用 +=
    public func += <T : Strideable>(inout lhs: T, rhs: T.Stride) {
      lhs = lhs.advancedBy(rhs)
    }
    

    (来自 swift/stdlib/public/core/Stride.swift 的 Swift 源代码版本,它在某种程度上与 Swift 2.2 相对应)。鉴于以下问答:
  • Trim end off of string in swift, getting error at runtime ,
  • Swift distance() method throws fatal error: can not increment endIndex ,

  • 我们可以怀疑我们可能需要使用 String.CharacterView.Index.advancedBy(_:limit:) 而不是上面的 ...advancedBy(_:)。然而,据我所知,next() 中的 StrideToGenerator 方法防止索引超过限制。

    编辑补充: 错误的来源似乎确实位于 next() 中的 StrideToGenerator 方法中:
    // ... in StrideToGenerator
    public mutating func next() -> Element? {
        if stride > 0 ? current >= end : current <= end {
            return nil
        }
        let ret = current
        current += stride /* <-- will increase current to larger or equal to end 
                                   if stride is large enough (even if this last current
                                   will never be returned in next call to next()) */
        return ret
    }
    

    即使 current 的最后一次更新永远不会返回(在下一次调用 next() 时),current 索引的最终推进值大于或等于 end 的值会产生上面的特定运行时错误,对于 Index 类型 String.CharacterView.Index
    (0..<4).startIndex.advancedBy(4) // OK, -> 4
    "foo".startIndex.advancedBy(4)   // fatal error: cannot increment endIndex
    

    这是否被视为错误,还是 String.CharacterView.Index 根本不打算(直接)符合 Stridable

    最佳答案

    简单地声明协议(protocol)一致性

    extension String.CharacterView.Index : Strideable { }
    

    编译是因为 String.CharacterView.Index 符合BidirectionalIndexTypeForwardIndexType/BidirectionalIndexTypeadvancedBy()distanceTo() 的默认方法实现
    根据 Strideable 的要求。
    Strideable 有默认的协议(protocol)方法实现
    对于 stride() :
    extension Strideable {
        // ...
        public func stride(to end: Self, by stride: Self.Stride) -> StrideTo<Self>
    }
    

    所以唯一“直接”实现的方法String.CharacterView.Index 是——据我所知——来自 successor()predecessor()BidirectionalIndexType 方法。

    正如您已经发现的,默认方法实现stride() 不适用于 String.CharacterView.Index

    但是总是可以为具体类型定义专用方法。对于使String.CharacterView.Index符合Strideable问题参见
    下面的 Vatsal Manot's answer 和评论中的讨论——我花了一段时间才明白他的意思:)

    这是 stride(to:by:)String.CharacterView.Index 方法的可能实现:
    extension String.CharacterView.Index {
        typealias Index = String.CharacterView.Index
    
        func stride(to end: Index, by stride: Int) -> AnySequence<Index> {
    
            precondition(stride != 0, "stride size must not be zero")
    
            return AnySequence { () -> AnyGenerator<Index> in
                var current = self
                return AnyGenerator {
                    if stride > 0 ? current >= end : current <= end {
                        return nil
                    }
                    defer {
                        current = current.advancedBy(stride, limit: end)
                    }
                    return current
                }
            }
        }
    }
    

    这似乎按预期工作:
    let str = "01234"
    str.startIndex.stride(to: str.endIndex, by: 2).forEach {
        print($0,str.characters[$0])
    } 
    

    输出
    0 0
    2 2
    4 4
    

    关于swift - 使 String.CharacterView.Index 符合 Strideable : fatal error when using stride(to:by:): "cannot increment endIndex ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205309/

    有关swift - 使 String.CharacterView.Index 符合 Strideable : fatal error when using stride(to:by:): "cannot increment endIndex "的更多相关文章

    1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

      我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

    2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

      我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

    3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

      为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

    4. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

      从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

    5. ruby - 续集在添加关联时访问many_to_many连接表 - 2

      我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

    6. ruby-on-rails - rails : How to make a form post to another controller action - 2

      我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

    7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

      我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

    8. ruby-on-rails - link_to 不显示任何 rails - 2

      我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

    9. ruby - 无法覆盖 irb 中的 to_s - 2

      我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

    10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

      我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

    随机推荐