如何在 SwiftUI 中打印()到 Xcode 控制台? [英] How to print() to Xcode console in SwiftUI?

查看:111
本文介绍了如何在 SwiftUI 中打印()到 Xcode 控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试在 SwiftUI 视图中调试时放置一个打印语句.

So I tried to put a print statement while debugging in a SwiftUI View.

print("landmark: \(landmark)")

在下面的正文中.

var body: some View {
    NavigationView {
        List {
            Toggle(isOn: $userData.showFavoritesOnly) {
                Text("Favorite only")
            }
            ForEach(landmarkData) { landmark in
                print("landmark: \(landmark)")
                if !self.userData.showFavoritesOnly || landmark.isFavorite {
                    NavigationButton(destination: LandmarkDetail(landmark: landmark)) {
                        LandmarkRow(landmark: landmark)
                    }
                }
            }
        }
       .navigationBarTitle(Text("Landmarks"))            
    }
}

编译器错误:

那么,在 SwiftUI 中打印到控制台的正确方法是什么?

So, what is the proper way to print to console in SwiftUI?

我让 Landmark 符合 CustomStringConvertible:

I made Landmark conform to CustomStringConvertible:

struct Landmark: Hashable, Codable, Identifiable, CustomStringConvertible {

var description: String { name+"\(id)" }

var id: Int
var name: String
.....

我仍然收到字符串不可转换为任何"错误.现在应该可以了吗?

I still get the "String is not convertible to any" error. Should it work now?

推荐答案

至少在 Xcode 12/Swift 5.3 中,您可以轻松地在函数构建器的任何位置添加打印语句,只需将其返回值存储在通配符中,有效地忽略它:

At least in Xcode 12/Swift 5.3, you can easily add a print statement anywhere in a function builder by simply storing its return value in a wildcard, effectively ignoring it:

let _ = print("hi!")

无需设置或其他详细说明!

No setup or other verbosity needed!

这篇关于如何在 SwiftUI 中打印()到 Xcode 控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆