Swift & the Objective-c Runtime

Source: Internet
Author: User

Association objects (Associated Objects)
extension Uiviewcontroller {Private structAssociatedkeys {Staticvar descriptivename ="Nsh_descriptivename"} var descriptivename:string? {        Get {            returnObjc_getassociatedobject (self, &associatedkeys.descriptivename) as?String}Set {            ifLet NewValue =newvalue {objc_setassociatedobject (self,&Associatedkeys.descriptivename, NewValue asNSString?, UInt (objc_association_retain_nonatomic))} }    }}

Method Cross (methods swizzling)
extension Uiviewcontroller { Public Override classFunc Initialize () {structStatic {Staticvar token:dispatch_once_t =0        }        //Make sure this isn ' t a subclass        ifSelf!==uiviewcontroller.self {return} dispatch_once (&static.token) {Let originalselector= Selector ("viewwillappear:") Let Swizzledselector= Selector ("nsh_viewwillappear:") Let Originalmethod=Class_getinstancemethod (self, originalselector) let Swizzledmethod=Class_getinstancemethod (self, swizzledselector) let Didaddmethod=Class_addmethod (self, originalselector, method_getimplementation (Swizzledmethod), method_gettypeencoding ( Swizzledmethod))ifDidaddmethod {Class_replacemethod (self, swizzledselector, method_getimplementation (Originalmethod), M Ethod_gettypeencoding (Originalmethod))}Else{method_exchangeimplementations (Originalmethod, Swizzledmethod); }        }    }    //MARK:-Method swizzlingfunc nsh_viewwillappear (animated:bool) {self.nsh_viewwillappear (animated)ifLet name =self.descriptivename {println ("viewwillappear: \ (name)")        } Else{println ("viewwillappear: \ (self)")        }    }}

Swift & the Objective-c Runtime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.