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)