SwiftUI 开发问题集锦

@高效码农  December 7, 2021

开发环境:

  • xcode版本:13.1
  • swift版本:5.5.1
  • swift-driver version: 1.26.9
  • Apple Swift version 5.5.1

一、缺少文件:

问题:

创建新项目后缺少了 AppDelegate.swiftSceneDelegate.swift 文件,那么在初始化一些SDK时,无法初始化

解决方案:

项目App.swift文件中加入init()方法:

@main
struct SwiftUIExampleApp: App {
    
    init() {
        // 可以在这里做初始化的操作
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

二、找不到Info.plist文件

问题:

在需要往info.plist文件中添加值时,找不到info.plist文件

解决方案:

info.plist 文件改为了xcode的配置,位置向导为;
Project → Targets → Info → Custom iOS Target Properties
2021-12-07T06:33:07.png

三、报错信息:Non-constant range: not an integer range

解决方案:

传递一个id参数

2021-12-07T09:27:12.png

原因Apple官方解释:
/// Creates an instance that generates Rotor content by combining, in order,
    /// individual Rotor content for each element in the data given to this
    /// `ForEach`.
    ///
    /// It's important that the `id` of a data element doesn't change unless you
    /// replace the data element with a new data element that has a new
    /// identity.
    ///
    /// - Parameters:
    ///   - data: The identified data that the ``ForEach`` instance uses to
    ///     create views dynamically.
    ///   - content: The result builder that generates Rotor content for each
    ///     data element.


添加新评论