You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift解析JSON时Xcode出现LLDB RPC server崩溃问题求助

JSON解析引发LLDB RPC server崩溃的问题解决

问题背景

需要解析的JSON结构:

{
    "weather": [
        {
            "id": 501,
            "main": "Rain",
            "description": "moderate rain",
            "icon": "10d"
        }
    ]
}

对应的Swift代码尝试解析时,Xcode抛出错误:

LLDB RPC server crashed

原代码如下:

import UIKit

struct Weather : Decodable {
    let id : Int
    let main : String
    let description : String
    let icon : String
}

let JSONdata = """
{
    "weather": [
        {
        "id": 501,
        "main": "Rain",
        "description": "moderate rain",
        "icon": "10d"
        }
   ]
}
"""

let decoder = JSONDecoder()

enum ItemType: String, Decodable {
    case weather
}

enum Item: Decodable {
    case weather(Weather)
    
    enum CodingKeys: CodingKey {
        case weather
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let type = try container.decode(ItemType.self, forKey: .weather)
        
        let singleContainer = try decoder.singleValueContainer()
        switch type {
        case .weather:
            self = .weather(try singleContainer.decode(Weather.self))
        }
    }
}

do {
    let respons = try JSONDecoder().decode([Item].self, from: String(JSONdata).data(using: .utf8)!)
    if case let .weather(d) = respons[0] {
        print(d.description)
    }
} catch {
    print("Error: ", error)
}

错误原因

  • 解码目标类型不匹配:JSON是一个包含weather数组的单个对象,但代码强行解码成[Item].self(Item数组),类型完全不匹配。
  • 自定义解码逻辑混乱:Item的初始化方法中,同时尝试获取keyed容器和singleValue容器,这两种容器不能从同一个解码器同时获取,直接导致解析逻辑混乱,触发LLDB的RPC服务崩溃。
  • 冗余的枚举设计:当前JSON结构简单,完全不需要Item和ItemType这类枚举,属于过度设计,反而引发问题。

修正后的代码

只需要贴合JSON层级定义结构体,就能正常解析:

import UIKit

struct Weather: Decodable {
    let id: Int
    let main: String
    let description: String
    let icon: String
}

// 定义顶层结构体,匹配JSON的根结构
struct WeatherResponse: Decodable {
    let weather: [Weather]
}

let JSONdata = """
{
    "weather": [
        {
            "id": 501,
            "main": "Rain",
            "description": "moderate rain",
            "icon": "10d"
        }
    ]
}
"""

do {
    guard let data = JSONdata.data(using: .utf8) else {
        print("字符串转Data失败")
        return
    }
    let response = try JSONDecoder().decode(WeatherResponse.self, from: data)
    // 获取数组中第一个天气对象
    if let firstWeather = response.weather.first {
        print(firstWeather.description)
        // 直接访问所有需要的字段
        print("ID: \(firstWeather.id), 天气类型: \(firstWeather.main), 图标: \(firstWeather.icon)")
    }
} catch {
    print("解析错误: ", error)
}

说明

修正后的逻辑简单直接:

  1. Weather结构体对应JSON数组里的单个天气对象
  2. WeatherResponse结构体对应JSON的根对象,包含weather数组字段
  3. 用JSONDecoder直接解码成WeatherResponse类型,就能正常读取id、main、description、icon所有字段的值,不会触发LLDB崩溃问题

内容的提问来源于stack exchange,提问作者user29783672

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:28:24