Skip to content

Instantly share code, notes, and snippets.

View FWEugene's full-sized avatar

Yevgeniy FWEugene

  • Future Workshops
View GitHub Profile
@FWEugene
FWEugene / libdispatch-efficiency-tips.md
Created August 7, 2019 08:22 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

I suspect most developers are using the libdispatch inefficiently due to the way it was presented to us at the time it was introduced and for many years after that, and due to the confusing documentation and API. I realized this after reading the 'concurrency' discussion on the swift-evolution mailing-list, in particular the messages from Pierre Habouzit (who is the libdispatch maintainer at Apple) are quite enlightening (and you can also find many tweets from him on the subject).

My take-aways are:

@FWEugene
FWEugene / Sorts.md
Created January 10, 2019 17:39
Sorts of all sorts

Sorts

Bubble sort

Complexity Space Stable
O(n^2) O(1) Yes
Until array is sorted compares adjacent pairs and swaps them if they are in the wrong order.
+ Easy to implement

Selection sort

Complexity Space Stable
@FWEugene
FWEugene / SwiftConcurrency.md
Created January 10, 2019 17:37
All about concurrency

Threads

Foundation offers a Thread class, internally based on pthread, that can be used to create new threads and execute closures.

// Detaches a new thread and uses the specified selector as the thread entry point.
Thread.detachNewThreadSelector(selector: Selector>, toTarget: Any, with: Any)

// Subclass
class MyThread: Thread {
import Foundation
extension String {
func replaceCharactersFromSet(characterSet: NSCharacterSet, replacementString: String = "") -> String {
return self.componentsSeparatedByCharactersInSet(characterSet).joinWithSeparator(replacementString)
}
}
//
// ConcurrentOperation.swift
// Yevgeniy Prokoshev
//
// Created by Yevgeniy Prokoshev on 14/08/2018.
// Copyright © 2018 Yevgeniy Prokoshev. All rights reserved.
//
import Foundation
@FWEugene
FWEugene / NSManagedObject+Extension.swift
Last active August 5, 2019 11:02
NSMAnagedObject Extension
//MARK: NSManagedObject Extension
public extension NSManagedObject {
public static func findOrCreate<T: NSManagedObject>(with keyedValues: [String: Any], context: NSManagedObjectContext) -> T {
let object: T? = self.findFirst(with: keyedValues, context: context)
if let found = object {
return found
} else {
var new: T!
context.performAndWait {
@FWEugene
FWEugene / String+LanguageDetector.swift
Last active August 3, 2021 11:36
Detecting Language
// The result is not guaranteed to be accurate. Typically, the function requires 200-400 characters to reliably guess the language of a string.
// Reference: [CFStringTokenizerCopyBestStringLanguage(_:_:)](https://developer.apple.com/reference/corefoundation/1542136-cfstringtokenizercopybeststringl)
//
import Foundation
extension String {
func guessLanguage() -> String {
let length = self.utf16.count
let languageCode = CFStringTokenizerCopyBestStringLanguage(self as CFString, CFRange(location: 0, length: length)) as String? ?? ""
#import <Foundation/Foundation.h>
#import "TextInputValidator.h"
@interface EmailStringValidator : NSObject <TextInputValidator>
@end
@FWEugene
FWEugene / swiftTips.swift
Last active November 18, 2022 21:21
Useful Swift Tips and Tricks
// This is a list of nice tips and tricks I discovered
//MARK: Tip #1
let task = urlSession.dataTask(with: url) { data, _, error
switch error {
case .error(let error as NSError) where error.code == NSURLErrorNotConnectedToInternet:
presenter.showOfflneError()
case .error(let error):