{ Objective-C }

  • Swift 方法桥接到 ObjC 时报 type cannot be represented

    | /

    工程里 SwiftObjective-C 混编已经是常态,经常遇到 Objective-C 代码去调用 Swift 里的方法。最新的 Swift 需要手动标注 @objc 才能把方法暴露。(印象中似乎在 Swift 2.x 的时代,是默认暴露)

    但经常会遇到 method cannot be marked @objc because the type of the parameter cannot be represented in objective-c 类似的错误,直觉为参数、返回值,无法从 Swift 桥接到 Objective-C,有时候一眼就知道缺了什么,有时候则要思考半天,这里简单整理一下几种常见的情况。

  • Quick Fix For Can't add self as subview

    | /

    I believe you can always find a lot of this kind of Crash: Can't add self as subview.

    We also know that we will never write code like add self as subview.

    In fact, in most cases, the push and pop pages with animation and no animation occur at the same time.

    Because we all know that this is often caused by the complexity of the page hierarchy and the unpredictable page jumps. So often choose to shelve this Crash helplessly.

    But we always think about how to eliminate this Crash. Whether it is from the perspective of user experience or from the aspect of Crash rate management.

  • Preprocessor Macros in Pods

    | /

    As mentioned earlier, we can inject macros into Pods from the outside by modifying the Podfile. You can refer to this article Inject TEST macros into Pods.

    Sometimes there are some macros that are expected to be used inside the Pods library we wrote ourselves.

    A common practice, just like ordinary business code, define a macro definition header file, and then import.

    But sometimes there are such scenarios, such as logging or providing some externally exposed APIs, you need to provide the version of the current library.

  • Inject TEST macros into Pods

    | /

    When we develop iOS applications, we often use CocoaPods for third-party library management.

    In addition to external third-party libraries, it is common for CocoaPods to be used as a library management tool for individuals or companies.

    We usually use macros such as DEBUG for environment-sensitive branch compilation.

    CocoaPods provides DEBUG macro injection by default to facilitate our local debugging.

    However, in actual development projects, in addition to using the DEBUG macro to identify local debugging, macros such as TEST or QA are also used to identify the test build package.

    At this time, we expect that the library in Pods can also inject TEST macros on demand.

  • weakSelf in dealloc

    | /

    We all know that it is generally recommended to do simple cleanup in the dealloc method.

    A very important reason is that in the process of destroying an object, if an operation such as weak is performed, it will cause a crash.

  • Inject cookies in WKWebView

    | /

    We often encounter scenarios where we need to inject cookies into WKWebView from the outside.

    There are several specific schemes, generally speaking, the choice needs to be made in combination with the actual scene.

    Here is a description of an actual scenario in a project I’ve encountered.

  • respondsToSelector Of Super

    | /

    respondsToSelector is often used to determine whether an instance implements a method for safe calling.

    But please don’t call the respondsToSelector method on super.

    Sometimes we only want to call a method of the parent class, but we only know that although the parent class implements a certain protocol, it does not necessarily implement the method in the protocol.