Created
          July 14, 2014 11:47 
        
      - 
      
 - 
        
Save SwiftStudies/e3cf1debe970f4a22b3e to your computer and use it in GitHub Desktop.  
    You can swizzle in Swift... http://www.swift-studies.com/blog/2014/7/13/method-swizzling-in-swift
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Cocoa | |
| // Only needed until we have class variables | |
| var __SwizzleSayHello = { (who:String) -> String in | |
| return "Hello, \(who)" | |
| } | |
| class Swizzle { | |
| //Only needed until we have class variables | |
| class var _sayHello : (String)->String { get{ return __SwizzleSayHello } set (swizzle) {__SwizzleSayHello = swizzle} } | |
| func sayHello(who:String)->String{ | |
| return Swizzle._sayHello(who) | |
| } | |
| } | |
| let immutableInstance = Swizzle() | |
| var mutableInstance = Swizzle() | |
| //Both print "Hello, World" | |
| println(immutableInstance.sayHello("World")) | |
| println(mutableInstance.sayHello("World")) | |
| Swizzle._sayHello = { (who:String) -> String in | |
| return "Howdy, \(who)" | |
| } | |
| //Both print "Howdy, World" | |
| println(immutableInstance.sayHello("World")) | |
| println(mutableInstance.sayHello("World")) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This is awesome. Method swizzle!