{ Swift }

  • 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,有时候一眼就知道缺了什么,有时候则要思考半天,这里简单整理一下几种常见的情况。

  • Swift构造函数(init)与属性观察器(didSet)

    | /

    最近工程里 SwiftObjective-C 混编的场景越来越多了,大家在编写 Swift 代码时,一般都会用 didSet 来作为 Objective-Csetter 的替代。但最近遇到好几起(自己和其他同事)因为 Swiftinit 里对属性赋值没有触发 didSet,进而导致结果不符合预期的问题,看来还是对 Swift 使用不熟练,这里稍微汇总下。(另外,末尾会附上 ChatGPT3.5 的回答情况)

  • Why State update doesn't work

    | /

    Recently, some personal apps have begun to try to use SwiftUI.

    Although some simple layouts, SwiftUI is indeed very efficient in development. But in actual development, many problems will be encountered.

    1. The development concept is different from UIKit, which leads to unaccustomed.
    2. Some components only have UIKit and need to be encapsulated.
    3. There will be compatibility issues with different versions of Xcode and operating systems.
    4. Sometimes some writing methods lead to actual effects that do not meet expectations.
    5. And so on.

    I have encountered a situation where the State change does not take effect, and it is also possible that my usage is not completely correct.

  • Something About Optional Assignment

    | /

    We all know that in Swift, optional chaining is an important feature that can help us write concise and elegant code.

    However, the optional assignment derived from the optional chain may have some strange phenomena when used.

  • 浅谈当Swift中defer和inout同时起作用

    | /

    本文针对 Swiftdeferinout 以及 return 时相互影响的时候的情况进行简单的分析,目的是更好的理解 deferinout 原理以及使用场合,减少踩坑和提高debug效率。

    本文中的示例代码,均为 Swift 4.2 版本