Skip to content

Instantly share code, notes, and snippets.

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))
}
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)
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
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) }
}
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
}
}
@ericdke
ericdke / dictionaryFromArrays.swift
Created November 3, 2015 18:56
Make a Dictionary from two arrays
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])
@ericdke
ericdke / dictionaryWithValues.swift
Last active November 3, 2015 18:57
Make a Dictionary from two arrays
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])
@ericdke
ericdke / splitBy.swift
Last active July 10, 2023 09:55
Swift: split array by chunks of given size
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])
}
}
}
@ericdke
ericdke / getAllCountryNames.swift
Last active January 9, 2016 22:37
Get list of country names
func getAllCountryNames() -> [String] {
let countryCodes = NSLocale.ISOCountryCodes()
return countryCodes.map() { countryCode in
return NSLocale.currentLocale().displayNameForKey(NSLocaleCountryCode, value : countryCode)!
}
}
print(getAllCountryNames())
@ericdke
ericdke / modelIdentifier.swift
Last active December 4, 2018 21:21
Mac OS X: find computer model identifier
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
}