Summary of common errors and warnings in development (16)
Objective-c Smart calls to incompatible swift methods
When you invoke a method in the Swift class in Objective-c, you will encounter a situation where the corresponding method cannot be found because some swift syntax does not support the conversion, as follows:
1 Import UIKit2 enumHttpstate {3 CaseSucced, Failed, Networkerror, Servererror, Others4 }5 classIlhttprequest:nsobject {6 classFunc Requestlogin (username:string, Password:string, callback: (State:httpstate)(Void)) {7Dispatch_async (Dispatch_get_global_queue (0,0), {(), Voidinch8Nsthread.sleepfortimeinterval (3)9Dispatch_async (Dispatch_get_main_queue (), {(), VoidinchTen Callback (state:HTTPState.Succed) One }) A }) - } -}
The corresponding $ (swift_module_name)-swift.h file is:
1 swift_class ("_ttc12ilswifttests13ilhttprequest")2@ Interface ilhttprequest:nsobject3 - (swift_nullability (nonnull) instancetype) init objc_ Designated_initializer; 4 @end
As can be seen from the above header file, the method Requestlogin uses the unsupported Swift enumeration, the conversion method is automatically ignored, there are two ways to skillfully solve similar problems:
Wrapping with supported Swift syntax
In the swift file, add a compatible wrapper method Wraprequestlogin, note that incompatible types or syntax cannot be used in this method
1 Import UIKit2 enumHttpstate:int {3 CaseSucced =0, Failed =1, Networkerror =2, Servererror =3, Others =44 }5 classIlhttprequest:nsobject {6 classFunc Requestlogin (username:string, Password:string, callback: (State:httpstate)(Void)) {7Dispatch_async (Dispatch_get_global_queue (0,0), {(), Voidinch8Nsthread.sleepfortimeinterval (3)9Dispatch_async (Dispatch_get_main_queue (), {(), VoidinchTen Callback (state:HTTPState.Succed) One }) A }) - } - classFunc Wraprequestlogin (username:string, Password:string, callback: (State:int)(Void)) { theSelf.requestlogin (UserName, Password:password) {(state)--(Void)inch - Callback (State:state.rawValue) - } - } +}
The corresponding $ (swift_module_name)-swift.h file is:
1 swift_class ( " _ttc12ilswifttests13ilhttprequest ) 2 @interface Ilhttprequest:nsobject 3 + (void ) Wraprequestlogin: (NSString * _ _nonnull) userName Password: (nsstring * __nonnull) Password callback: (void (^< Span style= "color: #000000;" > __nonnull) (Nsinteger)) callback; 4 - (Swift_nullability (nonnull) instancetype) Init Objc_designated_initializer; 5 @end
At this point, we can use the wrapping method directly in the Objective-c Wraprequestlogin
Clever use of inheritance
With inheritance you can support all swift types, the primary functionality is implemented in OBJECTIVE-C, and unsupported syntax is called in the Swift file, for example, Illoginsupercontroller as the parent class
1 @interfaceIlloginsupercontroller:uiviewcontroller2@property (Weak, nonatomic) Iboutlet Uitextfield *Usernamefield;3@property (Weak, nonatomic) Iboutlet Uitextfield *Passwordfield;4-(Ibaction) loginbuttonpressed: (ID) sender;5 @end6 ////////////////////////////////////////////////////////////////7 @implementationIlloginsupercontroller8-(Ibaction) loginbuttonpressed: (ID) Sender9 {Ten } One @end
Create Swift file, inherit from Illoginsupercontroller, call those unsupported syntax in this swift file
1 Import UIKit 2 class Illogincontroller:illoginsupercontroller {3 override func loginbuttonpressed (sender:anyobject!) {4in 5 // specific business logic 6 } 7 }8 }
iOS Development--Error summary & common errors and warnings in development summary (16)