Skip to content

Instantly share code, notes, and snippets.

@tobiastom
Forked from ttepasse/fuckingNSString.swift
Last active August 29, 2015 14:12
Show Gist options
  • Save tobiastom/fb7668c3ceda4258e643 to your computer and use it in GitHub Desktop.
Save tobiastom/fb7668c3ceda4258e643 to your computer and use it in GitHub Desktop.
//
// StringExtension.swift
// Tasty for iOS
//
// Created by Tobias Tom on 06.01.15.
// Copyright (c) 2015 succont e.K. All rights reserved.
//
import Foundation
extension String {
func firstLetterSectionName() -> String {
let fallback = "?!"
if self.isEmpty {
return fallback
}
let letters = NSCharacterSet.letterCharacterSet()
let numbers = NSCharacterSet.decimalDigitCharacterSet()
let firstLetter: NSString = self.substringToIndex( advance( self.startIndex, 1 ) )
let flAsUniChar: unichar = firstLetter.characterAtIndex(0)
var result = ""
switch ( flAsUniChar ) {
case 223: // Sonderbehandlung fürs ß. 223 ist dessen Wert als unichar
result = "S"
case let c where letters.characterIsMember(c):
let base = firstLetter.stringByFoldingWithOptions(
NSStringCompareOptions.DiacriticInsensitiveSearch,
locale: NSLocale.currentLocale()
)
result = base.uppercaseString
case let c where numbers.characterIsMember(c):
result = "0-9"
default:
result = fallback
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment