Skip to content

Instantly share code, notes, and snippets.

View werediver's full-sized avatar
💭
🦀

Raman Fedaseyeu werediver

💭
🦀
View GitHub Profile
@werediver
werediver / ViewTransition.swift
Last active July 8, 2020 10:19
View Transition mechanism for MVVM pattern (Swift, iOS)
import UIKit
// MARK: - Specification
protocol ViewType {
associatedtype ViewModel: ViewModelType
var viewModel: ViewModel { get }
@werediver
werediver / WiFiSsid.swift
Created July 14, 2016 12:47
Get the connected Wi-Fi network SSID on iOS (in Swift).
import Foundation
import SystemConfiguration.CaptiveNetwork
func getWiFiSsid() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
@werediver
werediver / FatalError+Ext.swift
Last active July 3, 2017 09:05
Better `fatalError()`.
import Foundation
func fatalError<T>(@autoclosure message: () -> String = "", file: StaticString = #file, line: UInt = #line) -> T {
fatalError(message(), file: file, line: line)
}
// Demo
protocol ResultType {
@werediver
werediver / concurrency.swift
Last active July 3, 2017 09:06
Limiting concurrent tasks.
import Foundation
func dispatch_async_batch(tasks: [() -> ()], limit: Int, completion: (() -> ())?) {
if tasks.count > 0 || completion != nil {
let q = dispatch_queue_create("dispatch_async_batch", DISPATCH_QUEUE_CONCURRENT);
let sema = dispatch_semaphore_create(limit);
dispatch_async(q, {
for task in tasks {
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
@werediver
werediver / TextFieldCell.swift
Last active August 20, 2016 07:34
How to get data from UITableView's cells?
import UIKit
import XCPlayground
// SKIP FOR NOW :)
protocol SmartCell {
associatedtype Model
static var reuseId: String { get }
@werediver
werediver / SimpleDataSource.swift
Created January 23, 2016 11:44
Pretty universal UITableView / UICollectionView data source implementation in Swift.
import UIKit
class BaseSimpleDataSource<Cell: ReusableView, Collection: CollectionType
where Collection.Index == Int,
Collection.Generator.Element: CollectionType,
Collection.Generator.Element.Index == Int,
Collection.Generator.Element.Generator.Element == Cell.Model>: NSObject {
typealias Item = Cell.Model
import UIKit
import PureLayout
extension UINib {
func instantiateWithOwner<T: AnyObject>(owner: AnyObject?, options: [NSObject : AnyObject]? = nil, type: T.Type) -> T {
let objs = self.instantiateWithOwner(owner, options: options)
return objs.findFirst { $0 is T } as! T
}
/// Cast the argument to the infered function return type.
func autocast<T>(some: Any) -> T? {
return some as? T
}
protocol Foo {
static func foo() -> Self
}
class Vehicle: Foo {
import Foundation
protocol ServiceLocator {
func getService<T>(type: T.Type) -> T?
func getService<T>() -> T?
}
extension ServiceLocator {
@werediver
werediver / LazyServiceLocator.swift
Last active March 18, 2020 10:32
Service Locator pattern implementation in Swift 2 with lazy (on demand) service initialization.
protocol ServiceLocator {
func getService<T>() -> T?
}
final class LazyServiceLocator: ServiceLocator {
/// Registry record
enum RegistryRec {
case Instance(Any)