小编Sco*_*des的帖子

如何在同一画布 SwiftUI 中预览多个视图

在此输入图像描述

struct CardDetailView_Previews: PreviewProvider {
    static var previews: some View {
        CardDetailView(viewModel: .init(labelLabelViewModel: LabelLabel.ViewModel(topText: "hello", bottomText: .lorem)))
            .previewDevice("iPhone 14 Pro Max")
        CardDetailView(viewModel: .init(labelLabelViewModel: LabelLabel.ViewModel(topText: "hello", bottomText: .lorem)))
            .previewInterfaceOrientation(.landscapeLeft)
            .previewDevice("iPhone 14 Pro Max")
        CardDetailView(viewModel: .init(labelLabelViewModel: LabelLabel.ViewModel(topText: "hello", bottomText: .lorem)))
            .previewInterfaceOrientation(.portrait)
            .previewDevice("iPhone SE (3rd generation)")
        CardDetailView(viewModel: .init(labelLabelViewModel: LabelLabel.ViewModel(topText: "hello", bottomText: .lorem)))
            .previewInterfaceOrientation(.landscapeLeft)
            .previewDevice("iPhone SE (3rd generation)")
        
    }
}

Run Code Online (Sandbox Code Playgroud)

我指定了要显示的多个版本。全部返回到相同的预览属性。但是,画布一次仅显示一个预览视图。我想同时看到这四个人。就像我在其他开发人员的观点中看到的那样。

在此输入图像描述

swiftui

23
推荐指数
2
解决办法
6177
查看次数

SwiftUI:如何从 NavigationView 访问 UINavigationController

我正在使用 SwiftUI 开发应用程序。该应用程序基于NavigationView.

我正在使用提供UIKit组件的第三方框架,并且该框架尚未更新以支持 SwiftUI。

一个框架方法需要一个类型的参数 UINavigationController

我怎样才能提供这个NavigationControlle由 SwiftUI 创建的框架?或者我如何创建一个UINavigationController将替换 SwiftUI 的默认值?

我阅读了https://developer.apple.com/tutorials/swiftui/interface-with-uikithttps://sarunw.com/posts/uikit-in-swiftui但这些似乎解决了另一个问题:他们解释了如何使用SwiftUI 应用程序中的 UIKit 组件。我的问题是反过来,我想使用 SwiftUI App 并访问底层 NavigationController 对象。

[更新] 本研讨会提供了实现我的解决方案的代码:https : //amplify-ios-workshop.go-aws.com/30_add_authentication/20_client_code.html#loginviewcontroller-swift

uikit uinavigationcontroller swiftui

18
推荐指数
2
解决办法
9888
查看次数

如何在 Xcode 14.3 中启用代码覆盖率?(看起来和其他版本不一样)

通常我会遵循以下步骤。基本上我会点击我的方案,然后编辑,然后测试,然后我会得到一个可以勾选的漂亮整洁的启用代码覆盖框。但是,对于 14.3,我看不到信息 | 选项等选项卡,而不是我看到这个视图。并且不存在启用代码覆盖率。通常,它位于构建配置和调试器之间。

14.3

在此输入图像描述

旧视图: 在此输入图像描述

我在这里也没有看到显示代码覆盖率:

在此输入图像描述

xcode code-coverage

15
推荐指数
2
解决办法
4336
查看次数

无法读取序列化诊断文件:无效文件:无效的诊断签名

在此处输入图片说明

我收到了这个奇怪的警告。我不确定是什么原因造成的。一个.dia文件扩展名据称表示有向核心图形文件。我没有添加,应用程序几乎没有用户界面。

xcode

12
推荐指数
2
解决办法
3989
查看次数

类属性“current”在异步上下文中不可用;Thread.current 不能在异步上下文中使用。这是 Swift 6 中的一个错误

我试图弄清楚在异步任务中我所处的线程或运行循环。如何从任务中获取线程或运行循环?

Task {
    do {
        print("line: ", #line, Thread.current)
        let image = try await self.fetchImage()
        print("line: ", #line, Thread.current)
    } catch {
        let fetch: FetchError = error as! FetchError
        print("line: ", #line, Thread.current)
    }
    print("line: ", #line, Thread.current)
}
print("line: ", #line)
Run Code Online (Sandbox Code Playgroud)

当我尝试我的典型方法(即Thread.current)时,我得到:

Class property 'current' is unavailable from asynchronous contexts; Thread.current cannot be used from async contexts.; this is an error in Swift 6.
Run Code Online (Sandbox Code Playgroud)

它仍然在运行,但警告让我相信我不能相信结果。

asynchronous async-await swift

8
推荐指数
1
解决办法
4109
查看次数

静态方法“buildBlock”要求“Button<Any>.Type”符合“View”

斯威夫伊新手。我该如何解决这个问题?


struct ContentView: View {
    var body: some View {
        HStack {
            VStack {
                Text("Hello World")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
            VStack {
                Button(action:  {
                    self.volumeCheck()
                }) {
                    Image("chimes")
                      .renderingMode(Image.TemplateRenderingMode?.init(Image.TemplateRenderingMode.original))

                }
                Button(action: {
                    self.volumeCheck()
                }) {
                    Text("Click chimes to test volume")
                }
            }
        }

    }

    func volumeCheck() {

    }
}


Run Code Online (Sandbox Code Playgroud)

报告

static method 'buildBlock' requires that 'Button<Any>.Type' conform to 'View'

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

failedToBuildDylib: /Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:18:20: error: static method 'buildBlock' requires that 'Button<Any>.Type' conform to 'View'
            VStack {
                   ^
SwiftUI.ViewBuilder:3:24: note: where …
Run Code Online (Sandbox Code Playgroud)

swiftui

7
推荐指数
2
解决办法
1万
查看次数

encodingOffset弃用

在我的应用程序中,我有一些代码来获取中的主机范围URL。看起来像这样:

private func rangeOfHost(text: String) -> NSRange? {
    let url = URL(string: text)
    if let host: String = url?.host {
        if let range = text.range(of: host) {
            return NSRange(
                location: range.lowerBound.encodedOffset,
                length: range.upperBound.encodedOffset - range.lowerBound.encodedOffset
            )
        }
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)

Xcode一直警告我'encodedOffset' is deprecated: encodedOffset has been deprecated as the most common usage is incorrect. Use utf16Offset(in:) to achieve the same behavior.。但是,我不清楚如何用这些建议替换那些encodingOffsets。有任何想法吗?

string nsrange swift

6
推荐指数
1
解决办法
1712
查看次数

初始化器要求 'init(json:)' 只能通过非最终类 'UIColor' 定义中的“必需”初始化器来满足

我正在尝试编写一个扩展来满足扩展中的协议,如下所示:

extension UIColor: JSONRepresentable {
    convenience init?(json: Any) {
        guard let colourArray = json as? [CGFloat] else {
            print("json was not an array of CGFloats")
            return nil
        }
    
        self.init(
            red: colourArray[0],
            green: colourArray[1],
            blue: colourArray[2],
            alpha: colourArray[3]
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Initializer requirement 'init(json:)' can only be satisfied by a required initializer in the definition of non-final class 'UIColor'.
Run Code Online (Sandbox Code Playgroud)

如果我添加required关键字,则会收到此错误

'required' initializer must be declared directly in class 'UIColor' (not in an extension).
Run Code Online (Sandbox Code Playgroud)

是否有原因或任何解决方法?

编辑:为了清楚起见,这是协议

protocol JSONRepresentable …
Run Code Online (Sandbox Code Playgroud)

protocols swift

5
推荐指数
1
解决办法
3665
查看次数

不能在不可变值上使用可变成员:'self' 在 SwiftUI 中是不可变的

对于以下代码,我收到以下错误。我不知道如何解决这个问题。如何调用volumeCheck()按钮单击?

struct ContentView: View {

    var player = AVAudioPlayer()

    var body: some View {
        HStack {
            Button(action: {
                self.volumeCheck()
            }) {
                Text("Click to test chimes volume")
            }
        }
    }

    mutating func volumeCheck() {
        guard let url = Bundle.main.url(
            forResource: "chimes",
            withExtension: "mp3"
            ) else { return }

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player.prepareToPlay()
            player.volume = Float(self.sliderValue)
            player.play()
        } catch let error {
            print(error.localizedDescription)
        }
        print("volume checked print")
    }
}
Run Code Online (Sandbox Code Playgroud)

swiftui

5
推荐指数
2
解决办法
5637
查看次数

art.scnassets代表什么?

我正在研究ARKit(iPhone框架),其中一个免费提供ARKit项目的文件夹是一个标题为的文件夹:art.scnassets.这些代表什么:art..scn.deatex

augmented-reality ios scenekit arkit

1
推荐指数
1
解决办法
1070
查看次数

在范围内找不到类型“UIViewRepresentable”

import SwiftUI
#if canImport(WebKit)
import WebKit
#endif

struct WebView: UIViewRepresentable {

    var url: URL

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

Run Code Online (Sandbox Code Playgroud)

我将我的应用程序创建为多平台应用程序。我添加了这个视图。但是我遇到了一些我无法弄清楚的错误。

Cannot find type 'UIViewRepresentable' in scope Cannot find type 'Context' in scope

我清除了构建文件夹。我的最低部署目标如下:

在此输入图像描述

我希望它针对以下方案运行:在此输入图像描述 在此输入图像描述

swiftui uiviewrepresentable

1
推荐指数
1
解决办法
3110
查看次数

未调用 reportNewIncomingCall 完成

reportNewIncomingCall完成参数不会被调用,这意味着我没有得到任何反馈,为什么我的VOIP应用程序不显示呼叫接收警报。

    func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false, completion: ((NSError?) -> Void)?) {
    // 1.
    let update = CXCallUpdate()
    update.remoteHandle = CXHandle(type: .phoneNumber, value: handle)
    update.hasVideo = hasVideo

    // 2.
    provider.reportNewIncomingCall(with: uuid, update: update) { error in
        if error == nil {
            // 3.
            let call = Call(uuid: uuid, handle: handle)
            self.callManager.add(call: call)
        }

        // 4.
        completion?(error as? NSError)
    }
}
Run Code Online (Sandbox Code Playgroud)

我是从Ray Wanderlich 教程中获取的

我尽可能准确地复制了代码。它适用于教程,但不适用于我的项目。

voip swift callkit

0
推荐指数
1
解决办法
803
查看次数

swift 中是否有不在范围内(~=)运算符?

Swift 有一个很酷的运算符,如果一个元素位于两个先前元素的范围内,则该运算符返回 true。是否有一个 not 运算符,如果最后一个元素不在前两个元素的范围内,它将返回 true?

的例子~=

/// If close is within ema and twentyBelowEma
if ema...twentyBelowEma ~= close {
   self = .aboveSupportAndEma
}
Run Code Online (Sandbox Code Playgroud)

swift

0
推荐指数
1
解决办法
404
查看次数