title: Make 2 contradictory methods work in drawRect
body: <p>I'm writing an app building elements consisting of CGPoints. I have 2 buttons: <code>makeRectangle</code> and <code>makeTriangle</code>. For building/drawing stage I use three methods for <strong>rectangle</strong> and three methods for <strong>triangle</strong> inside <code>drawRect</code>.</p>
  
  <p>I'm stuck with my code in <code>drawRect</code>. In <code>if-else-statement</code> each method swaps the building/drawing scheme for previous element every time a button pressed. </p>
  
  <p><strong>If I already have built rectangle</strong> and then I click <code>makeTriangle</code> button, I get new triangle but my rectangle turns into triangle with one unconnected point.</p>
  
  <p><strong>Is there a workaround</strong> or I shouldn't use <code>drawRect</code> method?</p>
  
  <p>Here's an SO post on the <code>drawRect</code> topic: <a href="https://stackoverflow.com/questions/14659563/to-drawrect-or-not-to-drawrect-when-should-one-use-drawrect-core-graphics-vs-su">To drawRect or not to drawRect</a></p>
  
  <p><a href="https://i.stack.imgur.com/bbk7W.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bbk7W.gif" alt="Animation of incorrectly-drawn trapezoid and triangle"></a></p>
  
  <p><a href="https://i.stack.imgur.com/zCX7z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zCX7z.jpg" alt="Image of correctly-drawn trapezoid and triangle"></a></p>
  
  <p>Element declaration:</p>
  
  <pre><code>enum Element {
      case point1(point: CGPoint)
      case point2(point: CGPoint)
      case point3(point: CGPoint)
      case point4(point: CGPoint)
  
      func coord() -&gt; [CGPoint] {    
          switch self {  
          case .point1(let point): return [point]
          case .point2(let point): return [point]
          case .point3(let point): return [point]
          case .point4(let point): return [point]
          }
      }
      func buildQuadPath(path: CGMutablePath) {
          switch self {
          case .point1(let point): CGPathMoveToPoint(path, nil, point.x, point.y)
          case .point2(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
          case .point3(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
          case .point4(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
          CGPathCloseSubpath(path)
          }
      }
      func buildTriPath(path: CGMutablePath) {
          switch self {
          case .point1(let point): CGPathMoveToPoint(path, nil, point.x, point.y)
          case .point2(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
          case .point3(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
          default:
          CGPathCloseSubpath(path)
          }
      }
  }
  </code></pre>
  
  <p>Methods for building and drawing triangle and rectangle:</p>
  
  <pre><code>func buildTriPath() -&gt; CGMutablePath {
      let path = CGPathCreateMutable()
      _ = array.map { $0.buildTriPath(path) }
      return path
  }
  func drawTriPath() {
      let path = buildTriPath()
      GraphicsState {
          CGContextAddPath(self.currentContext, path)
          CGContextStrokePath(self.currentContext)
      }
  }
  func drawTriFill() {
      let fill = buildTriPath()
      GraphicsState {
          CGContextAddPath(self.currentContext, fill)
          CGContextFillPath(self.currentContext)
      }
  }
  </code></pre>
  
  <p>///////////////////////////////////////////////////////</p>
  
  <pre><code>func buildQuadPath() -&gt; CGMutablePath {
      let path = CGPathCreateMutable()
      _ = array.map { $0.buildQuadPath(path) }
      return path
  }
  func drawQuadPath() {
      let path = buildQuadPath()
      GraphicsState {
          CGContextAddPath(self.currentContext, path)
          CGContextStrokePath(self.currentContext)
      }
  }
  func drawQuadFill() {
      let fill = buildQuadPath()
      GraphicsState {
          CGContextAddPath(self.currentContext, fill)
          CGContextFillPath(self.currentContext)
      }
  }
  </code></pre>
  
  <p>Two variables help determine whether button is pressed:</p>
  
  <pre><code>var squareB: Int = 0
  var triangleB: Int = 0
  
  @IBAction func makeTriangle(sender: AnyObject?) {
      ....................
      ....................
      triangleB += 1
      squareB = 0
  }
  @IBAction func makeRectangle(sender: AnyObject?) {
      ....................
      ....................
      triangleB = 0
      squareB += 1
  }
  </code></pre>
  
  <p><code>drawRect</code> method: </p>
  
  <pre><code>override func drawRect(dirtyRect: NSRect) {
      super.drawRect(dirtyRect)
  
      drawBG()
      GraphicsState { self.drawMyPoints() }
  
      if squareB &gt;= 1 &amp;&amp; triangleB == 0 {
          buildQuadPath()
          drawQuadPath()
          drawQuadFill()
          needsDisplay = true
      }
      else if triangleB &gt;= 1 &amp;&amp; squareB == 0 {
          buildTriPath()
          drawTriPath()
          drawTriFill()
          needsDisplay = true
      }
      drawBorder()
  }
  </code></pre>
  
  <p>...and at last a <code>Context.swift</code> file:</p>
  
  <pre><code>import Cocoa
  import CoreGraphics
  
  extension NSView {
  
      var currentContext : CGContext? {
  
          get {
  
              let unsafeContextPointer = NSGraphicsContext.currentContext()?.graphicsPort
  
              if let contextPointer = unsafeContextPointer {
              let opaquePointer = COpaquePointer(contextPointer)
              let context: CGContextRef = Unmanaged.fromOpaque(opaquePointer).takeUnretainedValue()
              return context }
              else { return nil }
          }
      }
  
      func GraphicsState(drawStuff: () -&gt; Void) {
          CGContextSaveGState(currentContext)
          drawStuff()
          CGContextRestoreGState(currentContext)
      }
  }
  
  //the end of code
  </code></pre>