【问题标题】:Xcode code completion suggests `printContent(_)` above `print(_)`Xcode 代码补全建议在 `print(_)` 上方使用 `printContent(_)`
【发布时间】:2021-12-03 21:20:36
【问题描述】:

使用 Xcode 13,在通用 Swift print() 函数上方的 Xcode 代码完成列表中,键入 print 的任何子字符串都会建议 printContent() 首先。

printContent(_ sender: Any?)
告诉您的应用打印可用内容。

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

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

printContent() 函数是什么,它是如何使用的?

它是否是 print() 的新更好替代品,证明其在 Xcode 中突出的代码完成位置是合理的?

如果没有,我该如何回到 Xcode 13 之前的行为并在列表中首先建议极其常见的 print() 函数?

【问题讨论】:

  • 如何回到 Xcode 13 之前的行为并在列表中首先建议极其常见的 print() 函数?每次都看到打印内容很烦人

标签: xcode nsobject code-completion uiresponder


【解决方案1】:

如果您的应用在其Info.plist 文件中包含UIApplicationSupportsPrintCommand 键,则人们可以使用调用printContent(_:) 的键盘快捷键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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多