Skip to main content
added 55 characters in body
Source Link
Nate Cook
  • 93.4k
  • 32
  • 220
  • 178

In beta 4, Swift's String.Index handling changed yet again -- you now can't supply an Int when a String.Index is expected. The way to handle it is by creating the String.Index you need using the advance method:

if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(advance(curPart.startIndex, 1)) } } if countElements(acronym as NSString).length > 2 { acronym = acronym.substringToIndex(advance(acronym.startIndex, 2)) } } 

This is all based on making sure Unicode strings are handled properly, but we - since different Unicode characters can have to assume this syntax will continue to evolve before Swift 1.0different sizes, pure integer indexing would hide the fact that Strings aren't random access.

In beta 4, Swift's String.Index handling changed yet again -- you now can't supply an Int when a String.Index is expected. The way to handle it is by creating the String.Index you need using the advance method:

if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(advance(curPart.startIndex, 1)) } } if (acronym as NSString).length > 2 { acronym = acronym.substringToIndex(advance(acronym.startIndex, 2)) } } 

This is all based on making sure Unicode strings are handled properly, but we have to assume this syntax will continue to evolve before Swift 1.0.

In beta 4, Swift's String.Index handling changed yet again -- you now can't supply an Int when a String.Index is expected. The way to handle it is by creating the String.Index you need using the advance method:

if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(advance(curPart.startIndex, 1)) } } if countElements(acronym) > 2 { acronym = acronym.substringToIndex(advance(acronym.startIndex, 2)) } } 

This is all based on making sure Unicode strings are handled properly - since different Unicode characters can have different sizes, pure integer indexing would hide the fact that Strings aren't random access.

Source Link
Nate Cook
  • 93.4k
  • 32
  • 220
  • 178

In beta 4, Swift's String.Index handling changed yet again -- you now can't supply an Int when a String.Index is expected. The way to handle it is by creating the String.Index you need using the advance method:

if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(advance(curPart.startIndex, 1)) } } if (acronym as NSString).length > 2 { acronym = acronym.substringToIndex(advance(acronym.startIndex, 2)) } } 

This is all based on making sure Unicode strings are handled properly, but we have to assume this syntax will continue to evolve before Swift 1.0.