【问题标题】:Im trying to make a my current weather temperature displayed in notification badge's我试图在通知徽章中显示我当前的天气温度
【发布时间】:2019-12-17 14:04:10
【问题描述】:

到目前为止,我正在尝试制作一个天气应用程序,但我正在处理通知,我希望我的当前温度显示在主屏幕上,显示为徽章。

到目前为止,我已经尝试显示tempLabel,但是我得到了错误

无法分配“UILabel”类型的值?输入“NSNumber?”。

self.locationLabel.text = jsonResponse["name"].stringValue
self.conditionImageView.image = UIImage(named: iconName)
self.conditionLabel.text = jsonWeather["main"].stringValue
self.temperatureLabel.text = "\(Int(round(jsonTemp["temp"].doubleValue)))"

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
self.dayLabel.text = dateFormatter.string(from: date)

let suffix = iconName.suffix(1)
if (suffix == "n") {
    self.setGreyGradientBackground()
} else {
    self.setBlueGradientBackground()
}

// Step 1: Ask for permission
let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
}

// Step 2: Create the notification content
let content = UNMutableNotificationContent()
content.title = "Daily Reminder"
content.body = "Dont forget to check today's weather"
content.badge = (temperatureLabel)

我希望我的temperatureLabel 以徽章格式显示。

self.temperatureLabel.text = "\(Int(round(jsonTemp["temp"].doubleValue)))"

let badgeCount: Int = temperatureLabel

我怎么会收到这个错误,

无法转换“UILabel”类型的值?到指定类型'Int'

【问题讨论】:

  • 也许可以阅读 UNMutableNotificationContent 的文档,特别是徽章是什么?

标签: swift xcode weather-api


【解决方案1】:

您正在混淆类型。错误说

  • temperatureLabelUILabel
  • badge 属性为NSNumber(可以是IntDouble

解决方案是先获取温度的Int值,然后将Int分配给徽章,并将其转换为String作为标签。

let badgeCount = Int(round(jsonTemp["temp"].doubleValue)
content.badge = badgeCount
self.temperatureLabel.text = "\(badgeCount)"

【讨论】:

  • 但是我对在“notification/content.badge = badgeCount”之后声明“jsonTemp”感到困扰,如果我只是将通知代码放低,我对我的代码有很多问题感到困扰。
  • 另外,如果我输入 "let badgeCount = Int(round(jsonTemp["temp"].doubleValue))" 我会收到以下错误 "Use of unresolved identifier 'jsonTemp'"
  • 当然代码必须在jsonTemp可用的范围内。从给定的代码中不清楚它是在哪里声明的。如果您在谈论OpenWeatherMap,它可能只是jsonWeather["main"]["temp"].doubleValue
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 2014-08-14
  • 2021-06-02
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
相关资源
最近更新 更多