# Swift access
OC use Swift method
1. Import Swift to the OC
#import "ProductModuleName-Swift.h"
The ProductModuleName- swift. h file contains declarations in the. Swift file, etc.
2.Include Swift Classes Using Forward Declarations
@class MySwiftClass; in .h
#import "ProductModuleName-Swift.h" in .m
3. Use @objc to expose OC or @objcMembers to expose the entire class to OC
The @objc identifier is automatically added to functions when implementing the OC interface and overriding OC methods
Swift use OC method
Add Bridging header when Swift is added for the first time
2. Transform macro definitions
Swift only supports simple numeric macro definitions. Complex macro definitions such as functions are not supported.
+ (CGFloat)ScreenWidth{
return SCREEN_WIDTH;
}
Copy the code
Macros are not recommended in Swift, simple macros can use let, complex macros can use static functions, inline functions, generics, etc.
Swift Style Guide
- Protocol
class MyViewController: UIViewController {
}
// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource {
}
// MARK: - UIScrollViewDelegate
extension MyViewController: UIScrollViewDelegate {
}
Copy the code
2.Optional
Use optional binding “if let”
if let myOptionalObject = myOptionalObject {
// do many things with myOptionalObject
}
Copy the code
3.[weak self] in closure
resource.request().onComplete { [weak self] response in
guard let self = self else {
return
}
let model = self.updateModel(response)
self.updateUI(model)
}
Copy the code
Attention
1. Bridging header Divides modules
2. Class delete keyword
3. OC attributes without nullable modifiers are converted to swift’s implicit selectable by default. , the system will not prompt to add? When unpacking, note that if the attribute value may be empty, add? To the end of the attribute. , such as the self. The property? .property
4. Initialization method
(1) The initialization function needs to make each attribute have an initial value, and the optional type automatically initializes to nil
(2) The constructor of the parent class is not integrated automatically, because the constructor of the parent class integrated by the subclass is easy to lead to incomplete initialization, that is, it does not meet the conditions (1)
(3) Specification of execution sequence
class childClass: fatherClass{ var childProperty:Any? init(childProperty:Any?) { self.childProperty = childProperty super.init() self.fatherProperty = ... }Copy the code
(4) convinence init
To customize initialization parameters, comply with the following principles:
- The convinence init function calls other initialization functions in the current class
- The Designated Init function of the parent class needs to be called
- Convinence Init needs to be finally called to the Designated Init function
5.setter
Swift does not recognize the setter method automatically generated by OC, and directly assigns the value to the property to call the setter method
6. The singleton
Global variables can be initialized automatically with dispatch_once by marking their constructor as private when they are defined as follows:
Static let shared = MyManager(string: someString) let string: string private init(string: someString) String) { self.string = string } }Copy the code
Mymanager.shared.method ()
to do
1. Whether kuAI-swift. h can be split
2. Perfect common macros
Finally, welcome to the mobile Knowledge Planet, where we will learn about new mobile features and discuss technical issues daily. Get a free ticket to join