IOS Runtime (Exchange of two methods)

Source: Internet
Author: User

For example, create the following code

NSString *str=Nil;     *url =[Nsurl urlwithstring:str];    NSLog (@ "%@", URL);

But you want to use your own method and make an empty judgment on the incoming string.

For example, I wrote a method

+ (Instancetype) Sjurlwithstr: (NSString *) str Alternative system method

[Nsurl URLWithString:]

However, if the project is now in the late stage will involve a lot of interfaces to import header files and modify the use of this method is inconvenient

Then you can create your own new URL classification in the classification using the runtime exchange of two systems and their own method of rapid implementation of the method of Exchange

#import"Nsurl+extension.h"#import<objc/message.h>@implementation Nsurl (Extension)+(void) load{//get the System method structure bodyMethod system = Class_getclassmethod ([selfclass], @selector (urlwithstring:)); //get your own method structure bodyMethod own = Class_getclassmethod ([selfclass], @selector (sjurlwithstr:)); //the urlwithstring of the Exchange method system and its own SJURLWITHSTR//Exchange own methods and system methodsmethod_exchangeimplementations (System, own); //When you use urlwithstring later, you are actually calling Sjurlwithstr.}+ (Instancetype) Sjurlwithstr: (NSString *) str{if(str==Nil) {NSLog (@"string is empty"); returnNil; }    //The following sjurlwithstr is actually using the urlwithstringNsurl *url =[Nsurl Sjurlwithstr:str]; returnURL;} @end

IOS Runtime (Exchange of two methods)

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.