OCProject introductionSwift

  1. Create one in the projectswiftClass, choosecreate Bridging Header

2.XcodeinBuild SettingsThe following configuration is automatically generated

  • Objective-C Bridging Header SwiftThe bridge file
  • Objective-C Generated Interface Header NameWant to visitSwiftmethodsOCImport in the classProductName-Swift.h(Enter manually without prompting and red before compiling), then compile:

  1. OCaccessSwift
    • In needSwiftOf the fileOCClass to import header filesProductName-Swift.h
    Class XTView: UIView {@objc @objc public func updateView() -> Int {return 0} required init? (coder: NSCoder) { return nil } } #import "xitu-Swift.h" XTView * xtView = [[XTView alloc] init]; [xtView updateView];Copy the code
  2. SwiftaccessOC
    • Bridge filesProductName-Bridging-Header.hIn the importSwiftneed-to-useOCfile

    import UIKit
    
    class SwiftClass01: NSObject {
    
        var con:ViewController?
    
        override init() {
            super.init()
    
            self.con = ViewController()
        }
    }
    Copy the code

OCThe macro inSwiftThe processing of

  • SwiftMacros are not defined in, but are replaced by global constants and global functions
Height #define kScreenWidth [UIScreen].bounds. Size. Height #define kScreenWidth [UIScreen] MainScreen] bounds. Size. Width Swift writing let kScreenW = UIScreen. Main. Bounds. Size. Width let kScreenH = UIScreen. Main. Bounds. Size. Height OC writing # define AUTO_WIDTH_FITSIZE (x) (x * kScreenW) / 375.0 Swift method func AUTO_WIDTH_FITSIZE(_ x: CGFloat) -> CGFloat {return (x*kScreenW)/375.0}Copy the code

OCProcessing of associated objects classified in Swift

  • SwiftThere is no classification in. Use extensions instead
@property (nonatomic, strong) NSString *futuresPromptKey; .m files const char * kTBFuturesDetaiKey = (void *) @ "kTBFuturesDetailPromptKeyContacts"; - (void)setFuturesPromptKey:(NSString *)futuresPromptKey { objc_setAssociatedObject(self, &kTBFuturesDetaiKey, futuresPromptKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (NSString *)futuresPromptKey { return objc_getAssociatedObject(self, &kTBFuturesDetaiKey); } private struct AssociateKeys {static var sectionHeaderItemKey: String = "sectionHeaderItemKey" } extension Array { public var sectionHeaderItem: XTTableViewCellItem? { get { return objc_getAssociatedObject(self, &AssociateKeys.sectionHeaderItemKey) as? XTTableViewCellItem } set { objc_setAssociatedObject(self, &AssociateKeys.sectionHeaderItemKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } } }Copy the code

OCIn the@synchronizedinSwiftThe use of

  • SwiftThere is no@synchronized, you can use global custom functions instead
OC writing @ synchronized (self themeCollection) {self. ThemeCollection. Themes = [themes copy]; self.themeCollection.defaultIndex = defaultIdx; self.themeCollection.selectedIndex = defaultIdx; } Swift func synchronized<T>(_ lock: Array<Any>, _ closure: () -> T) -> T { objc_sync_enter(lock) defer { objc_sync_exit(lock) } return closure() } return synchronized(items) { () -> Int in return self.items.count }Copy the code

OCIn theClassType in theSwiftThe use of

  • inOCTo deriveXTTableViewCellThe type of the class
    [TBTableViewCell class]
    Copy the code
  • inSwiftTo deriveXTTableViewCellThe type of the class
      @objc public func cellClass() -> AnyClass {
          return XTTableViewCell.self
      }
    Copy the code
  • swiftGets the type of an instance object or a class
    Anyobject. Type gets the source Type of an object.Type gets the Type value of A class. Class A {} let typeA: a.type = A.elfCopy the code
  • AnyClassinSwiftDefined in the
    typealias AnyClass = AnyObject.Type
    Copy the code

Circular reference inSwiftIn the processing

  • unownedway
    self.tableView.isLoadingBlock = {
      [unowned self]  (XTTableView) -> Bool in
      return self.isRefreshing
    }
    Copy the code

OCIs marked inSwiftThe use of

  • Use in OC:

    # pragma mark - add tag / / mark: add tag / * FIXME: add tag FIXME * / / / FIXME: add tag FIXME / /!!!!!! : Add tag!! / /??? : add??? // TODO: add TODO // #warning Add warningCopy the code
  • Use in Swift:

    // MARK: - Add tag // FIXME: Add tag 2 // TODO: Add tag 3Copy the code

APISystem Version Restrictions

  • OCThe use of
    If @ the available (iOS 11.0, *) { self.tableView.contentInsetAdjustmentBehavior = .never } else { self.automaticallyAdjustsScrollViewInsets = false }Copy the code
  • SwiftThe use of
    # if available (iOS 11.0, *) { self.tableView.contentInsetAdjustmentBehavior = .never } else { self.automaticallyAdjustsScrollViewInsets = false }Copy the code

reference

  • Configuration during Swift and OC co-programming
  • ANYCLASS, metatype and.self
  • Swift closure loop reference