Skip to content

通过抽离SwiftUI代码的方式提高预览速度

参考:
基于SwiftUI的组件化构建实践 | Doliant's Meditation World
Build programmatic UI with Xcode Previews - WWDC23 - Videos - Apple Developer
Build programmatic UI with Xcode Previews | WWDC NOTES

老项目太大,编译极慢,因为swiftui建立在编译完整个项目的基础上。 因此可以将SwiftUI相关的代码抽离出来。

坑遇到的问题问题:

  • 如何使用图片
  • 如何使用三方类库

UI代码抽离到CocoaPods

意外发现CocoaPods现在支持生成example项目:

bash
  BetterUI git:(repo-swift-ui)  pod lib create BetterUI
Cloning `https://github.com/CocoaPods/pod-template.git` into `BetterUI`.
Configuring BetterUI template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2024-01-16 09:55:47.835 defaults[2470:31536]
The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
If this is your first time we recommend running through with the guide:
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )

 Press return to continue.
.... 

[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via `pod repo remove master`. To suppress this warning please add `warn_for_unused_master_specs_repo => false` to your Podfile.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'BetterUI/Example/BetterUI.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
➜  BetterUI git:(repo-swift-ui) ✗
  BetterUI git:(repo-swift-ui)  pod lib create BetterUI
Cloning `https://github.com/CocoaPods/pod-template.git` into `BetterUI`.
Configuring BetterUI template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2024-01-16 09:55:47.835 defaults[2470:31536]
The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
If this is your first time we recommend running through with the guide:
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )

 Press return to continue.
.... 

[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via `pod repo remove master`. To suppress this warning please add `warn_for_unused_master_specs_repo => false` to your Podfile.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'BetterUI/Example/BetterUI.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
➜  BetterUI git:(repo-swift-ui) ✗

问题

建议指定swift版本

需要额外注意的是,调整swift版本和主工程一致,例如s.swift_version = '5.0'

CocoaPods中无法预览的问题

发现如果Deveplopment Pods引入了其他子依赖,SwiftUI Preview就无法使用了,好在前人已经给与解决方案:

修改主工程的Pods

ruby
use_frameworks!

# 处理Pods中无法预览的问题
# [Unable to see XCode/SwiftUI Previews within CocoaPods frameworks · Issue #9275 · CocoaPods/CocoaPods](https://github.com/CocoaPods/CocoaPods/issues/9275#issuecomment-691032405)
class Pod::Target::BuildSettings::AggregateTargetSettings
  alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

  def ld_runpath_search_paths
      return ld_runpath_search_paths_original unless configuration_name == "Debug"
      return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
  end
end

class Pod::Target::BuildSettings::PodTargetSettings
  alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

  def ld_runpath_search_paths
      return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
  end
end

target 'BetterUI_Example' do 
  ....
use_frameworks!

# 处理Pods中无法预览的问题
# [Unable to see XCode/SwiftUI Previews within CocoaPods frameworks · Issue #9275 · CocoaPods/CocoaPods](https://github.com/CocoaPods/CocoaPods/issues/9275#issuecomment-691032405)
class Pod::Target::BuildSettings::AggregateTargetSettings
  alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

  def ld_runpath_search_paths
      return ld_runpath_search_paths_original unless configuration_name == "Debug"
      return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
  end
end

class Pod::Target::BuildSettings::PodTargetSettings
  alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

  def ld_runpath_search_paths
      return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
  end
end

target 'BetterUI_Example' do 
  ....