GVKun编程网logo

Swift OpenWeatherMap API类型“任何”错误的值

5

对于想了解SwiftOpenWeatherMapAPI类型“任何”错误的值的读者,本文将是一篇不可错过的文章,并且为您提供关于axios没有从openweathermapapi、Callapi和ope

对于想了解Swift OpenWeatherMap API类型“任何”错误的值的读者,本文将是一篇不可错过的文章,并且为您提供关于axios 没有从 openweathermap api、Callapi 和 openweathermap、Flutter 按日期从 openweathermap 创建数据列表、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts的有价值信息。

本文目录一览:

Swift OpenWeatherMap API类型“任何”错误的值

Swift OpenWeatherMap API类型“任何”错误的值

如何解决Swift OpenWeatherMap API类型“任何”错误的值

嘿,我的快速代码给出了错误

“任何”类型的值没有下标

在此行

if let weatherArray = jsonObj.value(forKey: "weather") as? NSArray {
print("Type \\(weatherArray[0]["main"])")
}

我不知道如何解决它,我试图在网上搜索,但是我并没有找到解决方法

其余代码

        let session = URLSession.shared
        let weatherURL = url!
        let dataTask = session.dataTask(with: weatherURL) {
        (data: Data?,response: URLResponse?,error: Error?) in
        if let error = error {
            print("Error:\\n\\(error)")
            }
            
        else {
            if let data = data {
                
                let dataString = String(data: data,encoding: String.Encoding.utf8)
                print("All the weather data:\\n\\(dataString!)")
                if let jsonObj = try? JSONSerialization.jsonObject(with: data,options: .allowFragments) as? NSDictionary {
                    if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
                        if let temperature = mainDictionary.value(forKey: "feels_like") {
                            dispatchQueue.main.async {
                                print("Feels like: \\(temperature)°C")
                            }
                        }
                    }
                    if let weatherArray = jsonObj.value(forKey: "weather") as? NSArray {
                        print("Type \\(weatherArray[0]["main"])")
                
                    }

解决方法

if let weatherArray = jsonObj.value(forKey: "weather") as? NSArray {
    print("Type \\(weatherArray[0]["main"])")
}

您必须再次将项目投射到NSDictionary

if let weatherArray = jsonObj.value(forKey: "weather") as? NSArray,let inner = weatherArray[0] as? NSDictionary {
        print("Type \\(inner["main"])")
}
,

您的weatherArray是未广播值的数组,类似于[Any]。当您尝试评估weatherArray[0]["main"]时,您引用的是类型Any的第一个元素,并尝试使用下标来获取内容,但是Any并没有实现一个元素,因此会出现错误。

axios 没有从 openweathermap api

axios 没有从 openweathermap api

如何解决axios 没有从 openweathermap api

我正在尝试从 openweathermap api 获取数据。我很容易使用邮递员获得它,但我尝试使用带有 axios 请求的 nodejs 并且它以 400 状态代码响应。 我为它编写了以下代码-

  1. try {
  2. let a = ''london''
  3. const data = await axios.get(
  4. `api.openweathermap.org/data/2.5/weather?q=${a}&appid=${process.env.WEATHER_API_KEY}`
  5. )
  6. console.log(''the data is'',data)
  7. if (data) {
  8. console.log(''the data is'',data)
  9. res.status(201).json(data)
  10. }
  11. } catch (error) {
  12. console.log(''error'',error.message)
  13. res.status(403)
  14. res.json(error)
  15. }

解决方法

您需要在网址中添加 http://https://。没有它们,URL 的格式不正确,axios 认为它​​是一个相对 URL(例如 /local/api),但是因为您尝试在节点上执行它,它没有像在浏览器(站点起源)。

Callapi 和 openweathermap

Callapi 和 openweathermap

如何解决Callapi 和 openweathermap

当我获得温度值时,我认为格式有问题。当我调用这个参数时,我总是用“d0” f.e. 得到它。 “-0,39d0”。我该如何解决?我的代码:

  1. <category>
  2. <pattern>PADANIE</pattern>
  3. <template>
  4. <think>
  5. <set name="weather_t">
  6. <callapi>
  7. <url>http://api.openweathermap.org/data/2.5/weather</url>
  8. <method>GET</method>
  9. <query name="appid"><secret name="openweathermap_secret_appid"/></query>
  10. <query name="q">zyrardow</query>
  11. <query name="units">metric</query>
  12. <query name="lang">pl</query>
  13. <filter type="jsonpath">$.main.temp</filter>
  14. </callapi>
  15. </set>
  16. </think>
  17. <get name="weather_t"/>.
  18. </template>
  19. </category>

如果我使用 jsonpath 而不是 filter 结果是一样的。

  1. <category>
  2. <pattern>PADANIE</pattern>
  3. <template>
  4. <think>
  5. <set var="weather_t">
  6. <callapi>
  7. <url>http://api.openweathermap.org/data/2.5/weather</url>
  8. <method>GET</method>
  9. <query name="appid"><secret name="openweathermap_secret_appid"/></query>
  10. <query name="q">zyrardow</query>
  11. <query name="units">metric</query>
  12. <query name="lang">pl</query>
  13. </callapi>
  14. </set>
  15. </think>
  16. <jsonpath><path>$.main.temp</path><get var="weather_t"/></jsonpath>
  17. </template>
  18. </category>

bot answer example

Flutter 按日期从 openweathermap 创建数据列表

Flutter 按日期从 openweathermap 创建数据列表

如何解决Flutter 按日期从 openweathermap 创建数据列表

我以 json 的形式从 openweathermap 中每隔 3 小时获取 5 天的天气数据。现在我正在制作一个数组,当我只选择我需要的数据时,但是如何按日期将它组合成一个数组或对象。也就是属于一天的所有数据?

  1. static List<Weather> fromForecastJson(Map<String,dynamic> json) {
  2. final weathers = List<Weather>();
  3. for (final item in json[''list'']) {
  4. weathers.add(Weather(
  5. time: item[''dt''],temperature: intTodouble(
  6. item[''main''][''temp''],),description: item[''weather''][0][''description''],iconCode: item[''weather''][0][''icon'']
  7. ));
  8. }
  9. return weathers;
  10. }

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts

如何解决iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

我不断收到来自随机用户的随机崩溃报告。不幸的是,我无法定期重现这一点。用户说崩溃是在 discussionViewController 中随机发生的。所有崩溃报告都有类似的内容:

0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916

这是完整的崩溃日志和崩溃的线程:

Hardware Model:      iphone11,6
Process:             APPNAME [11770]
Path:                /private/var/containers/Bundle/Application/.../APPNAME.app/APPNAME
Identifier:          ----
Version:             62 (62)
AppStoretools:       12E262
AppVariant:          1:iphone11,6:13
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           ---- [1824]


Date/Time:           2021-06-17 12:07:01.4346 +1000
Launch Time:         2021-06-17 12:06:56.4993 +1000
OS Version:          iPhone OS 14.6 (18F72)
Release Type:        User
Baseband Version:    3.04.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010 -> 0x0000000000000010 (possible pointer authentication failure)
VM Region Info: 0x10 is not in any region.  Bytes before following region: 4339515376
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102a7c000-102a94000 [   96K] r-x/r-x SM=COW  ...APPNAME.app/APPNAME

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL,Code 0xb
Terminating Process: exc handler [11770]
Triggered by Thread:  3


Thread 3 name:
Thread 3 Crashed:
0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916
4   APPNAME                             0x0000000102ad09d4 thunk for @escaping @callee_guaranteed (@guaranteed Data?,@guaranteed NSURLResponse?,@guaranteed Error?) -> () + 132 (<compiler-generated>:0)
5   CFNetwork                       0x00000001a1b0a3dc __40-[__NSURLSessionLocal taskForClassInfo:]_block_invoke + 540 (LocalSession.mm:687)
6   CFNetwork                       0x00000001a1b1c768 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 244 (LocalSessionTask.mm:584)
7   libdispatch.dylib               0x00000001a10d1a84 _dispatch_call_block_and_release + 32 (init.c:1466)
8   libdispatch.dylib               0x00000001a10d381c _dispatch_client_callout + 20 (object.m:559)
9   libdispatch.dylib               0x00000001a10db004 _dispatch_lane_serial_drain + 620 (inline_internal.h:2557)
10  libdispatch.dylib               0x00000001a10dbc34 _dispatch_lane_invoke + 456 (queue.c:3862)
11  libdispatch.dylib               0x00000001a10e64bc _dispatch_workloop_worker_thread + 764 (queue.c:6589)
12  libsystem_pthread.dylib         0x00000001ed04a7a4 0x1ed047000 + 14244
13  libsystem_pthread.dylib         0x00000001ed05174c 0x1ed047000 + 42828

我已验证 discussionViewController.fetchPostData() 不会强制解开任何可选选项,没有 try! 并且在任何地方都使用 [weak self]self?。该函数非常大,所以我很难缩小崩溃发生的范围。

今天关于Swift OpenWeatherMap API类型“任何”错误的值的讲解已经结束,谢谢您的阅读,如果想了解更多关于axios 没有从 openweathermap api、Callapi 和 openweathermap、Flutter 按日期从 openweathermap 创建数据列表、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts的相关知识,请在本站搜索。

本文标签: