Xcode代码补全提示 `printContent(_)`以上 `print(_)`

标签 xcode nsobject code-completion uiresponder

使用 Xcode 13,键入 print 的任何子字符串建议 printContent() 在 Xcode 代码完成列表中位于常见的 Swift print() 之上> 函数。

printContent first in list

printContent(_ sender: Any?)
Tells your app to print available content.

“跳转到定义”显示以下声明,2021 年 iOS 15 的新声明:

public protocol UIResponderStandardEditActions : NSObjectProtocol {
    
    // ...
    
    @available(iOS 15.0, *)
    optional func printContent(_ sender: Any?)
}

什么是 printContent() 函数以及如何使用它?

它是否以任何方式更好地替代 print(),证明其在 Xcode 中突出的代码完成位置是合理的?

如果不是,我怎样才能回到 Xcode 13 之前的行为并在列表中首先建议极其常见的 print() 函数?

最佳答案

如果您的应用在其 Info.plist 文件中包含 UIApplicationSupportsPrintCommand 键,人们可以使用键盘快捷键 Command-P 从您的应用打印,它调用 打印内容(_:)。您还可以将 printContent(_:) 设置为对其他打印相关控件(例如工具栏上的打印按钮)的操作。

override func printContent(_ sender: Any?) {
    let info = UIPrintInfo.printInfo()
    info.outputType = .photo
    info.orientation = .portrait
    info.jobName = modelItem.title
    
    let printInteractionController = UIPrintInteractionController()
    printInteractionController.printInfo = info
    printInteractionController.printingItem = modelItem.image
    
    let completionHandler: UIPrintInteractionController.CompletionHandler = {
        (controller: UIPrintInteractionController, completed: Bool, error: Error?) in
        if let error = error {
            Logger().error("Print failed due to an error: \(error.localizedDescription)")
        }
    }
    
    if traitCollection.userInterfaceIdiom == .pad {
        if let printButton = navigationItem.rightBarButtonItem {
            printInteractionController.present(from: printButton, animated: true, completionHandler: completionHandler)
        }
    } else {
        printInteractionController.present(animated: true, completionHandler: completionHandler)
    }
}

Apple developer link

关于Xcode代码补全提示 `printContent(_)`以上 `print(_)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69588633/

相关文章:

swift - 如何在 SwiftUI 中初始化预览提供程序中的数据

xcode - 升级到 Xcode 8 但缺少 svn 状态指示器

ios - 使用 NSObject 确定挂起的操作

php - 使用 Doctrine ORM : Custom repository

python - PyCharm 代码完成某些元素周围的红色矩形?

php - Vim:将 omnicomplete 显示的方法限制为 ctags 中存在的方法

XCode 7.3 代码标记错误(在 Swift 中)

swift - 使用 Swift 将 CLLocationCoordinate2d 转换为街道地址

ios - 从 NSObject 重新加载 UITableView

objective-c - 符合 NSObject 的类中 -self 方法的目的是什么?