Skip to main content
added 41 characters in body
Source Link
GoZoner
  • 70.8k
  • 20
  • 100
  • 148

The error is in advance(s.startIndex, 4) as you cannot advance eyond the end index:

 1> var s = "dog" s: String = "dog" 2> var i1 = advance(s.startIndex, 4) fatal error: can not increment endIndex i1: String.Index = { _base = { /* ... */ } /* ... */ } Execution interrupted. Enter Swift code to recover and continue. Enter LLDB commands to investigate (type :help for assistance.) 

You avoid this by providing an end index as:

 3> var i1 = advance(s.startIndex, 4, s.endIndex) i1: String.Index = { _base = { /* ... */ } /* ... */ } 

and then:

 4> s.substringToIndex(i1) $R0: String = "dog" 

at least for Swift1.2, in Xcode6-Beta3.

The error is in advance(s.startIndex, 4) as you cannot advance eyond the end index:

 1> var s = "dog" s: String = "dog" 2> var i1 = advance(s.startIndex, 4) fatal error: can not increment endIndex i1: String.Index = { _base = { /* ... */ } /* ... */ } Execution interrupted. Enter Swift code to recover and continue. Enter LLDB commands to investigate (type :help for assistance.) 

You avoid this by providing an end index as:

 3> var i1 = advance(s.startIndex, 4, s.endIndex) i1: String.Index = { _base = { /* ... */ } /* ... */ } 

and then:

 4> s.substringToIndex(i1) $R0: String = "dog" 

The error is in advance(s.startIndex, 4) as you cannot advance eyond the end index:

 1> var s = "dog" s: String = "dog" 2> var i1 = advance(s.startIndex, 4) fatal error: can not increment endIndex i1: String.Index = { _base = { /* ... */ } /* ... */ } Execution interrupted. Enter Swift code to recover and continue. Enter LLDB commands to investigate (type :help for assistance.) 

You avoid this by providing an end index as:

 3> var i1 = advance(s.startIndex, 4, s.endIndex) i1: String.Index = { _base = { /* ... */ } /* ... */ } 

and then:

 4> s.substringToIndex(i1) $R0: String = "dog" 

at least for Swift1.2, in Xcode6-Beta3.

Source Link
GoZoner
  • 70.8k
  • 20
  • 100
  • 148

The error is in advance(s.startIndex, 4) as you cannot advance eyond the end index:

 1> var s = "dog" s: String = "dog" 2> var i1 = advance(s.startIndex, 4) fatal error: can not increment endIndex i1: String.Index = { _base = { /* ... */ } /* ... */ } Execution interrupted. Enter Swift code to recover and continue. Enter LLDB commands to investigate (type :help for assistance.) 

You avoid this by providing an end index as:

 3> var i1 = advance(s.startIndex, 4, s.endIndex) i1: String.Index = { _base = { /* ... */ } /* ... */ } 

and then:

 4> s.substringToIndex(i1) $R0: String = "dog"