Ken*_*ong 4 ios swift watchkit
我在实现 Apple 的基本示例代码时遇到了麻烦,如下所示
这是我的代码;这是非常“教科书上的例子”。我当前收到错误“'init(named:)' 不可用”
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)
content.sound = UNNotificationSound(named: "test.aiff");
content.categoryIdentifier = "myCategory"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "test.aiff", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
编辑:在尝试建议后,这是代码,但它不起作用。它给出“init(named:)”不可用
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)
content.sound = UNNotificationSound.init(named: "test.aiff")
content.categoryIdentifier = "myCategory"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "test.aiff", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
https://developer.apple.com/documentation/usernotifications/unnotificationsoundname
let soundName = UNNotificationSoundName("test.aiff")
content.sound = UNNotificationSound(named: soundName)
Run Code Online (Sandbox Code Playgroud)