Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 将数据发送到3D触摸快捷方式_Swift_Swift2_3dtouch - Fatal编程技术网

Swift 将数据发送到3D触摸快捷方式

Swift 将数据发送到3D触摸快捷方式,swift,swift2,3dtouch,Swift,Swift2,3dtouch,我正在构建一个天气应用程序,我希望能够在用户激活快捷菜单(通过主屏幕上的3D触摸)时看到天气数据(如温度)。我希望天气数据显示在快捷方式中,这样用户就不必输入应用程序来检查温度。以下是用于检索天气数据的代码,如果需要,我将发布更多代码: struct ForecastService { let forecastAPIKey: String let forecastBaseURL: NSURL? init(apiKey: String) { forecastAPIKey = a

我正在构建一个天气应用程序,我希望能够在用户激活快捷菜单(通过主屏幕上的3D触摸)时看到天气数据(如温度)。我希望天气数据显示在快捷方式中,这样用户就不必输入应用程序来检查温度。以下是用于检索天气数据的代码,如果需要,我将发布更多代码:

struct ForecastService {
  let forecastAPIKey: String
  let forecastBaseURL: NSURL?
  init(apiKey: String) {
    forecastAPIKey = apiKey
    forecastBaseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
  }

  func getForecast(lat: Double, long: Double, completion: (CurrentWeather? -> Void)) {
    if let forecastURL = NSURL(string: "\(lat),\(long)", relativeToURL: forecastBaseURL) {
        let networkOperation = NetworkOperation(url: forecastURL)

        networkOperation.downloadJSONFromURL {
            (let JSONDictionary) in
            let currentWeather = self.currentWeatherFromJSON(JSONDictionary)
            completion(currentWeather)
        }
    } else {
        print("Could not construct a valid URL")
    }
  }

  func currentWeatherFromJSON(jsonDictionary: [String: AnyObject]?) -> CurrentWeather? {
    if let currentWeatherDictionary = jsonDictionary?["currently"] as? [String: AnyObject] {
        return CurrentWeather(weatherDictionary: currentWeatherDictionary)
    } else {
        print("JSON Dictionary returned nil for 'currently' key")
        return nil
    }
  }
}//end struct
您应该创建一个标题设置为要显示的天气条件的数组,然后将应用程序的标题设置为包含该项目的数组。例如:

let item = UIApplicationShortcutItem(type:"showCurrentConditions", localizedTitle:"64° Sunny")
UIApplication.sharedApplication().shortcutItems = [item]
请注意,“type”参数是一个任意字符串,您的应用程序代理只需要在用户访问时能够识别它。

您应该创建一个名称设置为要显示的天气条件的字符串,然后将应用程序的名称设置为包含该项目的数组。例如:

let item = UIApplicationShortcutItem(type:"showCurrentConditions", localizedTitle:"64° Sunny")
UIApplication.sharedApplication().shortcutItems = [item]
请注意,“type”参数是一个任意字符串,您的应用程序代理只需要在用户访问时能够识别它