This file contains hidden or 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
| extension NSCharacterSet { | |
| var characters:[String] { | |
| var chars = [String]() | |
| for plane:UInt8 in 0...16 { | |
| if self.hasMemberInPlane(plane) { | |
| for (var c:UTF32Char = UInt32(plane) << 16; c < (UInt32(plane) + 1) << 16; c += 1) { | |
| if self.longCharacterIsMember(c) { | |
| let s = NSString(bytes: &c, length: 4, encoding: NSUTF32LittleEndianStringEncoding)! | |
| chars.append(String(s)) | |
| } |
This file contains hidden or 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
| func getHostName(completion:(name:String)->()) { | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { | |
| if let name = NSHost.currentHost().localizedName { | |
| completion(name: name) | |
| } | |
| } | |
| } | |
| getHostName { name in | |
| print(name) |
This file contains hidden or 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
| extension String { | |
| var isNumeric: Bool { | |
| let nums: Set<Character> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] | |
| return Set(self.characters).isSubsetOf(nums) | |
| } | |
| } | |
| "No numbers here".isNumeric // false | |
| "123".isNumeric // true | |
| "Hello world 123".isNumeric // false |
This file contains hidden or 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
| extension String { | |
| func matchesBetweenPairs(of char: Character) -> [String] { | |
| let str = "(?<=\\\(char))[^ ]+(?=\\\(char))" | |
| let regex = try! NSRegularExpression(pattern: str, options: .caseInsensitive) | |
| let rng = NSRange(location: 0, length: self.characters.count) | |
| let matches = regex.matches(in: self, options: [], range: rng) | |
| return matches.map { (self as NSString).substring(with: $0.range) } | |
| } | |
This file contains hidden or 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
| extension String { | |
| var acronym:String { | |
| let chSet = NSMutableCharacterSet.punctuationCharacterSet() | |
| chSet.addCharactersInString("\n\r ") | |
| let words = self.componentsSeparatedByCharactersInSet(chSet) | |
| let letters = words.flatMap { $0.characters.first }.map { String($0) } | |
| return letters.joinWithSeparator("").uppercaseString | |
| } | |
| } |
This file contains hidden or 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
| extension Dictionary { | |
| init(keys: [Key], values:[Value]) { | |
| self.init() | |
| zip(keys, values).forEach { (k, v) in self[k] = v } | |
| } | |
| } | |
| let result = Dictionary(keys: ["name", "age"], values: ["eric", 42]) |
This file contains hidden or 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
| extension CollectionType where Generator.Element: Hashable { | |
| func dictionaryWithValues<U>(values: [U]) -> [Generator.Element:U] { | |
| var dict: [Generator.Element:U] = [:] | |
| zip(self, values).forEach { (k, v) in dict[k] = v } | |
| return dict | |
| } | |
| } | |
| let result = ["name", "age"].dictionaryWithValues(["eric", 42]) |
This file contains hidden or 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
| let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | |
| extension Array { | |
| func splitBy(subSize: Int) -> [[Element]] { | |
| return 0.stride(to: self.count, by: subSize).map { startIndex in | |
| let endIndex = startIndex.advancedBy(subSize, limit: self.count) | |
| return Array(self[startIndex ..< endIndex]) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| func getAllCountryNames() -> [String] { | |
| let countryCodes = NSLocale.ISOCountryCodes() | |
| return countryCodes.map() { countryCode in | |
| return NSLocale.currentLocale().displayNameForKey(NSLocaleCountryCode, value : countryCode)! | |
| } | |
| } | |
| print(getAllCountryNames()) |
This file contains hidden or 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
| func modelIdentifier() -> String? { | |
| let service: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) | |
| let cfstr = "model" as CFString | |
| if let model = IORegistryEntryCreateCFProperty(service, cfstr, kCFAllocatorDefault, 0).takeUnretainedValue() as? NSData { | |
| if let nsstr = NSString(data: model, encoding: NSUTF8StringEncoding) { | |
| return nsstr as String | |
| } | |
| } | |
| return nil | |
| } |