问题:
当尝试通过例如跨越 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 )? 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.Index 与 Stridable 的一致性而弹出的错误? +1 以外的步骤中迭代字符的主题有关,即使两个问题不同,也值得包含在此问题中。String.CharacterView.Index type 描述了一个字符位置,并且:Comparable (因此, Equatable ), advancedBy(_:) 和 distanceTo(_:) 的实现。 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() structure 的 StrideToGenerator 方法,可能是当此方法在 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 相对应)。鉴于以下问答: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 符合BidirectionalIndexType 和 ForwardIndexType/BidirectionalIndexType 有 advancedBy() 和 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的问题参见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/
我正在尝试测试是否存在表单。我是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
我在从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""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
从给定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
我正在使用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].有没有一种方法可以
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我遵循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
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我在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)
我正在尝试从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