-
-
Save tobiastom/fb7668c3ceda4258e643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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