菜鸟教程小白 发表于 2022-12-12 21:42:13

ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始化程序


                                            <p><p>我正在使用一个示例项目来处理新通知,从 Swift 2.2 过渡到 Swift 2.3,也从 iOS 9 过渡到 iOS 10。我正在使用下面的代码,取自 <a href="http://cleanswifter.com/ios-10-local-notifications/" rel="noreferrer noopener nofollow">this example</a> :</p>

<pre><code>@available(iOS 10.0, *)
func setupAndGenerateLocalNotification() {
    // Register an Actionable Notification
    let highFiveAction = UNNotificationAction(identifier: NotificationActions.HighFive.rawValue, title: &#34;High Five&#34;, options: [])

    //next line is an error:
    let category = UNNotificationCategory(identifier: &#34;wassup&#34;, actions: , minimalActions: , intentIdentifiers: [], options: [.customDismissAction])
    //Cannot invoke initializer for type &#39;UNNotificationCategory&#39; with an argument list of type &#39;(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)&#39;

    UNUserNotificationCenter.current().setNotificationCategories()

    let highFiveContent = UNMutableNotificationContent()
    highFiveContent.title = &#34;Wassup?&#34;
    highFiveContent.body = &#34;Can I get a high five?&#34;

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    let highFiveRequestIdentifier = &#34;sampleRequest&#34;
    let highFiveRequest = UNNotificationRequest(identifier: highFiveRequestIdentifier, content: highFiveContent, trigger: trigger)
    UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(highFiveRequest) { (error) in
      // handle the error if needed
      print(error)
    }
}
</code></pre>

<p>我得到的错误是</p>

<pre><code>Cannot invoke initializer for type &#39;UNNotificationCategory&#39; with an argument list of type &#39;(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)&#39;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你没有提到你的错误是什么,但我怀疑你复制的代码是为比你安装的更早的测试版编写的。</p>

<p>我在工作中使用的是 Swift 3,而不是 Swift 2.3。在那里,从 Xcode 8 beta 5 开始,您应该将 <code>UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest</code> 更改为 <code>UNUserNotificationCenter.current().add</code>,并删除 <code>minimalActions: ,</code> 来自您的 <code>UNNotificationCategory</code>。可能只有后者可能适用于 Swift 2.3?</p>

<p>我希望这能让你继续前进,但请注意:这可能会在 iOS 10 最终版之前再次发生变化。特别是,我怀疑 <code>current()</code> 可能会变成 <code>current</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始化程序,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38849693/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38849693/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始化程序