解决方案代码
let center = UNUserNotificationCenter.current()
print(center.getPendingNotificationRequests(completionHandler: { error in
// error handling here
}))
Run Code Online (Sandbox Code Playgroud)
我的原帖:
我正在尝试通过UNUserNotificationCenter获取待处理通知的列表UIApplication.shared.scheduledLocalNotifications
.
这是我正在使用的代码:
let center = UNUserNotificationCenter.current()
print(UNUserNotificationCenter.getPendingNotificationRequests(center))
Run Code Online (Sandbox Code Playgroud)
但是这会打印"(功能)".getPendingNotificationRequests需要一个UNUserNotificationCenter参数,我想不出它可能是什么.
谢谢
我想将 Assets.xcassets 中的图像附加到通知中。我一直在寻找解决方案大约一个小时,这似乎是唯一的方法:
func createLocalUrl(forImageNamed name: String) -> URL? {
let fileManager = FileManager.default
let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let url = cacheDirectory.appendingPathComponent("\(name).png")
guard fileManager.fileExists(atPath: url.path) else {
guard
let image = UIImage(named: name),
let data = UIImagePNGRepresentation(image)
else { return nil }
fileManager.createFile(atPath: url.path, contents: data, attributes: nil)
return url
}
return url
}
Run Code Online (Sandbox Code Playgroud)
来源:我可以从 XCAssets 包中获取 NSURL 吗?
这似乎有点矫枉过正。但是,据我所知,这是获取可以使用的 URL 的唯一方法:
let imageURL = createLocalUrl(forImageNamed: "TestImage")
let attachment = try! UNNotificationAttachment(identifier: "image", url: imageURL!, options: …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个单元测试来检查我从日期到字符串的转换是否成功。
我通过以下方式将其转换为字符串:
func convertDateToString(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date)
}
Run Code Online (Sandbox Code Playgroud)
并通过以下方式将其转换回:
func convertStringToDate(string: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.date(from: string)!
}
Run Code Online (Sandbox Code Playgroud)
如果您尝试在转换前日期和转换后日期使用 Equatable 协议,它表示它们不相同。但是,如果您将 pre 和 post 日期都转换为字符串并进行比较,则它们是 Equatable 的。这是我在前后日期运行 XCAssertEqual 时所说的:
XCTAssertEqual failed: ("2020-01-22 19:35:40 +0000") is not equal to ("2020-01-22 19:35:40 +0000")
Run Code Online (Sandbox Code Playgroud)
这对我来说看起来非常相似。我什至尝试将转换前的日期转换为字符串并返回以检查日期是否相等并且它们仍然不相等