working on it ...

Filters

Explore Public Snippets

Sort by

Found 15k snippets

    public by ManuelAurora modified Jul 27, 2018  392  2  4  0

    LinkedList (Collection)

     class Node<Value> {
        var value: Value
        var next: Node?
        
        init(value: Value, next: Node? = nil) {
            self.value = value
            self.next = next
        }
     }
     
     //MARK: - CustomStringConvertible
     extension Node: CustomStringConvertible {
        var description: String {
            guard let next = next else { return "\(value)" }
            return "\(value) -> " + String(describing: next) + " "
        }
     }
     
    struct LinkedList<Value> {
        var head: Node<Value>?
        var tail: Node<Value>?
        
        init() {}
        var isEmpty: Bool {
            return head == nil
        }
        
        mutating func copyNodes() {
            guard !isKnownUniquelyReferenced(&head) else { return }
            guard var oldNode = head else { return }
            
            head = Node(value: oldNode.value)
            var newNode = head
            
            while let nextOldNode = oldNode.next {
                newNode!.next = Node(value: nextOldNode.value)
                newNode = newNode!.next
                oldNode = nextOldNode
            }
            tail = newNode
        }
        
        mutating func push(_ value: Value) {
            copyNodes()
            head = Node(value: value, next: head)
            if tail == nil { tail = head }
        }
        
        mutating func append(_ value: Value) {
            copyNodes()
            guard !isEmpty else {
                push(value); return
            }
            
            tail!.next = Node(value: value)
            tail = tail!.next
        }
        
        func node(at index: Int) -> Node<Value>? {
            var currentNode = head
            for _ in 0..<index {
                currentNode = currentNode?.next
            }
            return currentNode
        }
        
        @discardableResult
        mutating func insert(value: Value, after node: Node<Value>) -> Node<Value> {
            copyNodes()
            guard tail !== node else { append(value); return tail! }
            let newNode = Node(value: value, next: node.next)
            node.next = newNode
            return newNode
        }
        
        @discardableResult
        mutating func pop() -> Value? {
            copyNodes()
            defer {
                head = head?.next
                if isEmpty { tail = nil }
            }        
            return head?.value
        }
        
        @discardableResult
        mutating func removeLast() -> Value? {
            copyNodes()
            guard let head = head else { return nil }
            guard head.next != nil else { return pop() }
            
            var prev = head
            var current = head
            
            while let next = current.next {
                prev = current
                current = next
            }
            
            prev.next = nil
            tail = prev
            
            return current.value
        }
        
        @discardableResult
        mutating func remove(after node: Node<Value>) -> Value? {
            copyNodes()
            defer {
                if node.next === tail {
                    tail = node
                }
                let nodeToRemove = node.next
                node.next = nodeToRemove?.next
            }
            return node.next?.value
        }
     }
     
     extension LinkedList: CustomStringConvertible {
        var description: String {
            guard let head = head else { return "Empty list" }
            return String(describing: head)
        }
     }
     
     extension LinkedList: Collection {
        struct Index: Comparable {
            var node: Node<Value>?
            
            static func ==(lhs: Index, rhs: Index) -> Bool {
                switch (lhs.node, rhs.node) {
                case let (left?, right?):
                    return left.next === right.next
                case (nil, nil):
                    return true
                default: return false
                }
            }
            
            static func <(lhs: Index, rhs: Index) -> Bool {
                guard lhs != rhs else { return false }
                let nodes = sequence(first: lhs.node) { $0?.next }
                return nodes.contains { $0 === rhs.node }
            }
        }
        
        var startIndex: Index {
            return Index(node: head)
        }
        
        var endIndex: Index {
            return Index(node: tail?.next)
        }
        
        func index(after i: Index) -> Index {
            return Index(node: i.node?.next)
        }
        
        subscript(position: Index) -> Value {
            return position.node!.value
        }
     }
    

    public by ManuelAurora modified Jul 26, 2018  363  0  5  0

    Prototype pattern. Copying protocol

    // Enter here the actual content of the snippet.						
    public protocol Copying: class {
        init(_ prototype: Self)
    }
    
    extension Copying {
        public func copy() -> Self {
            return type(of: self).init(self)
        }
    }
    				
    extension Array where Element: Copying {
      public func deepCopy() -> [Element] {
        return map { $0.copy() }
      }
    }		

    public by pradithya modified Dec 9, 2017  496  0  4  0

    Creating Alert with Action

    let controller = UIAlertController()
    controller.title = "Hey!"
    controller.message = "Don't Worry Be Happy!"
            
    let okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default) { (alert: UIAlertAction) in
        self.dismiss(animated:true)
    }
            
    controller.addAction(okAction)
    self.present(controller, animated: true, completion: nil)
    						

    public by pradithya modified Dec 9, 2017  364  1  4  0

    Launching Activity View Controller

    let image = UIImage()
    let viewController = UIActivityViewController(activityItems:[image], applicationActivities: nil)
    self.present(viewController, animated:true, completion: nil);
    						

    public by pradithya modified Dec 9, 2017  551  1  4  0

    Launching Image Picker

    self.present(UIImagePickerController(), animated:true, completion: nil);
    																		

    public by Santiago modified Aug 14, 2017  382  3  4  0

    CoreLocatonManager + Rx

    Reactive extension for Core Location manager
    class RxCLLocationManagerDelegateProxy: DelegateProxy, CLLocationManagerDelegate, DelegateProxyType {
      class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
        let locationManager: CLLocationManager = object as! CLLocationManager
        locationManager.delegate = delegate as? CLLocationManagerDelegate
      }
      class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
        let locationManager: CLLocationManager = object as! CLLocationManager
        return locationManager.delegate
      }
    }
    
    extension Reactive where Base: CLLocationManager {
      var delegate: DelegateProxy {
        return RxCLLocationManagerDelegateProxy.proxyForObject(base)
      }
    
      var didUpdateLocations: Observable<[CLLocation]> {
        return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:didUpdateLocations:)))
          .map { parameters in
            return parameters[1] as! [CLLocation]
          }
      }
    }
    

    public by Santiago modified Aug 14, 2017  316  0  4  0

    MKMapView + Rx

    Reactive extension for map view
    class RxMKMapViewDelegateProxy: DelegateProxy, MKMapViewDelegate, DelegateProxyType {
      class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
        let mapView: MKMapView = (object as? MKMapView)!
        return mapView.delegate
      }
    
      class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
        let mapView: MKMapView = (object as? MKMapView)!
        mapView.delegate = delegate as? MKMapViewDelegate
      }
    }
    
    extension Reactive where Base: MKMapView {
      public var delegate: DelegateProxy {
        return RxMKMapViewDelegateProxy.proxyForObject(base)
      }
    
      public func setDelegate(_ delegate: MKMapViewDelegate) -> Disposable {
        return RxMKMapViewDelegateProxy.installForwardDelegate(
          delegate,
          retainDelegate: false,
          onProxyForObject: self.base
        )
      }
    
      var overlays: UIBindingObserver<Base, [MKOverlay]> {
        return UIBindingObserver(UIElement: self.base) { mapView, overlays in
          mapView.removeOverlays(mapView.overlays)
          mapView.addOverlays(overlays)
        }
      }
    
      public var regionDidChangeAnimated: ControlEvent<Bool> {
        let source = delegate
          .methodInvoked(#selector(MKMapViewDelegate.mapView(_:regionDidChangeAnimated:)))
          .map { parameters in
            return (parameters[1] as? Bool) ?? false
          }
        return ControlEvent(events: source)
      }
    }
    

    public by Santiago modified Aug 14, 2017  290  0  5  0

    BindableType protocol implementation

    protocol BindableType
    {
      associatedtype ViewModelType
    
      var viewModel: ViewModelType! { get set }
    
      func bindViewModel()
    }
    
    extension BindableType where Self: UIViewController 
    {
      mutating func bindViewModel(to model: Self.ViewModelType) {
        viewModel = model
        loadViewIfNeeded()
        bindViewModel()
      }
    }

    public by Santiago modified Aug 14, 2017  305  0  4  0

    UINavigationControllerDelegate + Rx

    Rx extension for navigation controller delegate
    class RxNavigationControllerDelegateProxy: DelegateProxy, DelegateProxyType, UINavigationControllerDelegate {
    
      static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
        guard let navigationController = object as? UINavigationController else {
          fatalError()
        }
        return navigationController.delegate
      }
    
      static func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
        guard let navigationController = object as? UINavigationController else {
          fatalError()
        }
        if delegate == nil {
          navigationController.delegate = nil
        } else {
          guard let delegate = delegate as? UINavigationControllerDelegate else {
            fatalError()
          }
          navigationController.delegate = delegate
        }
      }
    }
    
    extension Reactive where Base: UINavigationController {
      /**
         Reactive wrapper for `delegate`.
         For more information take a look at `DelegateProxyType` protocol documentation.
         */
      public var delegate: DelegateProxy {
        return RxNavigationControllerDelegateProxy.proxyForObject(base)
      }
    }

    public by Santiago modified Aug 14, 2017  279  1  5  0

    SceneCoordinatorType protocol

    protocol SceneCoordinatorType 
    {
      init(window: UIWindow)
    
      /// transition to another scene
      @discardableResult
      func transition(to scene: Scene, type: SceneTransitionType) -> Observable<Void>
    
      /// pop scene from navigation stack or dismiss current modal
      @discardableResult
      func pop(animated: Bool) -> Observable<Void>
    }
    
    extension SceneCoordinatorType {
      @discardableResult
      func pop() -> Observable<Void> {
        return pop(animated: true)
      }
    }
    
    enum Scene {
      case tasks(TasksViewModel)
      case editTask(EditTaskViewModel)
    }
    
    enum SceneTransitionType {
      // you can extend this to add animated transition types,
      // interactive transitions and even child view controllers!
    
      case root       // make view controller the root view controller
      case push       // push view controller to navigation stack
      case modal      // present view controller modally
    }
    
    extension Scene {
      func viewController() -> UIViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        switch self {
        case .tasks(let viewModel):
          let nc = storyboard.instantiateViewController(withIdentifier: "Tasks") as! UINavigationController
          var vc = nc.viewControllers.first as! TasksViewController
          vc.bindViewModel(to: viewModel)
          return nc
    
        case .editTask(let viewModel):
          let nc = storyboard.instantiateViewController(withIdentifier: "EditTask") as! UINavigationController
          var vc = nc.viewControllers.first as! EditTaskViewController
          vc.bindViewModel(to: viewModel)
          return nc
        }
      }
    }
    
    						
    • Public Snippets
    • Channels Snippets