Swift JSONDecoder decode error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x5e48)
Swift JSONDecoder decode error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x5e48)
我正在尝试在 Playground 中将下面的 JSON 和 运行 解码为 EXC_BAD_ACCESS 错误。理想情况下,我想解码 Person 结构中的所有属性,但是,我现在只能解码那些未被注释的属性。一旦我取消注释任何一次注释,它就会抛出 error Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x5e48).。我在这里做错了什么?我做了很多 Google 搜索,找不到任何答案。
在此先感谢您的帮助!
struct Address: Decodable {
var line1: String
var city: String
var region: String
var postalCode: String
var countryCode: String
}
struct CityCoordinate : Decodable {
var latitude: Double
var longitude: Double
}
struct Thumbnail: Decodable {
var url: String
var width: Int
var height: Int
}
struct Headshot: Decodable {
var url: String
var width: Int
var height: Int
var sourceUrl: String
var thumbnails: [Thumbnail]?
}
struct Meta : Decodable {
var id: String
}
struct Person : Decodable {
//var newPatients: Bool?
var address: Address
var description: String?
var name: String
var cityCoordinate: CityCoordinate
//var baseURL: String?
// var c_baseURL: String?
// var headhshot: Headshot?
// var insuranceAccepted: [String]?
//var expertises: [String]?
// var meta: Meta
enum CodingKeys : String, CodingKey {
//case newPatients = "acceptingNewPatients"
case address
case description
case name
case cityCoordinate
// case headhshot
// case insuranceAccepted
//case baseURL = "c_baseURL"
//case expertises = "c_expertises"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
//self.newPatients = try container.decodeIfPresent(Bool.self, forKey: .newPatients) ?? false
self.address = try container.decode(Address.self, forKey: .address)
self.description = try container.decode(String.self, forKey: .description)
self.name = try container.decode(String.self, forKey: .name)
self.cityCoordinate = try container.decode(CityCoordinate.self, forKey: .cityCoordinate)
// self.insuranceAccepted = try? container.decodeIfPresent([String].self, forKey: .insuranceAccepted) ?? []
//self.baseURL = try? container.decodeIfPresent(String.self, forKey: .baseURL) ?? ""
// self.headhshot = try? container.decodeIfPresent(Headshot.self, forKey: .headhshot)
// self.expertises = try? container.decodeIfPresent([String].self, forKey: .expertises) ?? []
}
}
let json2 = """
{
"acceptingNewPatients": true,
"address": {
"line1": "111 7th Ave",
"city": "New York",
"region": "NY",
"postalCode": "10005",
"countryCode": "US"
},
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"name": "John Doe",
"cityCoordinate": {
"latitude": 41.78200149536133,
"longitude": -72.83170318603516
},
"c_baseURL": "https://some-url.dummy.com/my-profile/current",
"headshot": {
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"width": 199,
"height": 199,
"sourceUrl": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"thumbnails": [{
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/196x196.png",
"width": 196,
"height": 196
}]
},
"insuranceAccepted": ["Finibus", "anything", "hidden", "consectetur" ],
"meta": {
"accountId": "967081498762345229",
"uid": "Nd28v0",
"id": "5119-13270",
"timestamp": "2021-03-24T12:10:36",
"labels": ["61280", "61288", "84273", "117907", "122937", "126160"],
"folderId": "351068",
"schemaTypes": ["therefore", "popular", "commodo", "voluptatem"],
"language": "en",
"countryCode": "US",
"entityType": "informationProfessional"
}
}
""".data(using: .utf8)!
let person = try JSONDecoder().decode(Person.self, from:json2)
print(person)
我认为有两三个问题导致了我认为的崩溃,第一个是缺少元数据,第二个是没有 c_expertise
最后,当 Headshot?
是在 init
中可选,您需要使用 try
而不是 try?
,因为它正在解码一个可选密钥,在这些更改之后:
struct Address: Decodable {
var line1: String
var city: String
var region: String
var postalCode: String
var countryCode: String
}
struct CityCoordinate : Decodable {
var latitude: Double
var longitude: Double
}
struct Thumbnail: Decodable {
var url: String
var width: Int
var height: Int
}
struct Headshot: Decodable {
var url: String
var width: Int
var height: Int
var sourceUrl: String
var thumbnails: [Thumbnail]?
}
struct Meta : Decodable {
var id: String
}
struct Person : Decodable {
var newPatients: Bool?
var address: Address
var description: String?
var name: String
var cityCoordinate: CityCoordinate
var baseURL: String?
var c_baseURL: String?
var headhshot: Headshot?
var insuranceAccepted: [String]?
var expertises: [String]?
var meta: Meta?
enum CodingKeys : String, CodingKey {
case newPatients = "acceptingNewPatients"
case address
case description
case name
case cityCoordinate
case headhshot
case insuranceAccepted
case baseURL = "c_baseURL"
// case expertises = "c_expertises"
case meta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.newPatients = try container.decodeIfPresent(Bool.self, forKey: .newPatients) ?? false
self.address = try container.decode(Address.self, forKey: .address)
self.description = try container.decode(String.self, forKey: .description)
self.name = try container.decode(String.self, forKey: .name)
self.cityCoordinate = try container.decode(CityCoordinate.self, forKey: .cityCoordinate)
self.insuranceAccepted = try? container.decodeIfPresent([String].self, forKey: .insuranceAccepted) ?? []
self.baseURL = try? container.decodeIfPresent(String.self, forKey: .baseURL) ?? ""
self.headhshot = try container.decodeIfPresent(Headshot.self, forKey: .headhshot)
//self.expertises = try? container.decodeIfPresent([String].self, forKey: .expertises) ?? []
self.meta = try container.decodeIfPresent(Meta.self, forKey: .meta)
}
}
let json2 = """
{
"acceptingNewPatients": true,
"address": {
"line1": "111 7th Ave",
"city": "New York",
"region": "NY",
"postalCode": "10005",
"countryCode": "US"
},
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"name": "John Doe",
"cityCoordinate": {
"latitude": 41.78200149536133,
"longitude": -72.83170318603516
},
"c_baseURL": "https://some-url.dummy.com/my-profile/current",
"headshot": {
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"width": 199,
"height": 199,
"sourceUrl": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"thumbnails": [{
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/196x196.png",
"width": 196,
"height": 196
}]
},
"insuranceAccepted": ["Finibus", "anything", "hidden", "consectetur" ],
"meta": {
"accountId": "967081498762345229",
"uid": "Nd28v0",
"id": "5119-13270",
"timestamp": "2021-03-24T12:10:36",
"labels": ["61280", "61288", "84273", "117907", "122937", "126160"],
"folderId": "351068",
"schemaTypes": ["therefore", "popular", "commodo", "voluptatem"],
"language": "en",
"countryCode": "US",
"entityType": "informationProfessional"
}
}
""".data(using: .utf8)!
do {
let person = try JSONDecoder().decode(Person.self, from:json2)
print(person)
} catch let error {
print(error)
}
它打印 Person
,请试一试,如果抛出任何错误,请在项目中尝试而不是 Playground
或在线编译器。
我正在尝试在 Playground 中将下面的 JSON 和 运行 解码为 EXC_BAD_ACCESS 错误。理想情况下,我想解码 Person 结构中的所有属性,但是,我现在只能解码那些未被注释的属性。一旦我取消注释任何一次注释,它就会抛出 error Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x5e48).。我在这里做错了什么?我做了很多 Google 搜索,找不到任何答案。
在此先感谢您的帮助!
struct Address: Decodable {
var line1: String
var city: String
var region: String
var postalCode: String
var countryCode: String
}
struct CityCoordinate : Decodable {
var latitude: Double
var longitude: Double
}
struct Thumbnail: Decodable {
var url: String
var width: Int
var height: Int
}
struct Headshot: Decodable {
var url: String
var width: Int
var height: Int
var sourceUrl: String
var thumbnails: [Thumbnail]?
}
struct Meta : Decodable {
var id: String
}
struct Person : Decodable {
//var newPatients: Bool?
var address: Address
var description: String?
var name: String
var cityCoordinate: CityCoordinate
//var baseURL: String?
// var c_baseURL: String?
// var headhshot: Headshot?
// var insuranceAccepted: [String]?
//var expertises: [String]?
// var meta: Meta
enum CodingKeys : String, CodingKey {
//case newPatients = "acceptingNewPatients"
case address
case description
case name
case cityCoordinate
// case headhshot
// case insuranceAccepted
//case baseURL = "c_baseURL"
//case expertises = "c_expertises"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
//self.newPatients = try container.decodeIfPresent(Bool.self, forKey: .newPatients) ?? false
self.address = try container.decode(Address.self, forKey: .address)
self.description = try container.decode(String.self, forKey: .description)
self.name = try container.decode(String.self, forKey: .name)
self.cityCoordinate = try container.decode(CityCoordinate.self, forKey: .cityCoordinate)
// self.insuranceAccepted = try? container.decodeIfPresent([String].self, forKey: .insuranceAccepted) ?? []
//self.baseURL = try? container.decodeIfPresent(String.self, forKey: .baseURL) ?? ""
// self.headhshot = try? container.decodeIfPresent(Headshot.self, forKey: .headhshot)
// self.expertises = try? container.decodeIfPresent([String].self, forKey: .expertises) ?? []
}
}
let json2 = """
{
"acceptingNewPatients": true,
"address": {
"line1": "111 7th Ave",
"city": "New York",
"region": "NY",
"postalCode": "10005",
"countryCode": "US"
},
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"name": "John Doe",
"cityCoordinate": {
"latitude": 41.78200149536133,
"longitude": -72.83170318603516
},
"c_baseURL": "https://some-url.dummy.com/my-profile/current",
"headshot": {
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"width": 199,
"height": 199,
"sourceUrl": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"thumbnails": [{
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/196x196.png",
"width": 196,
"height": 196
}]
},
"insuranceAccepted": ["Finibus", "anything", "hidden", "consectetur" ],
"meta": {
"accountId": "967081498762345229",
"uid": "Nd28v0",
"id": "5119-13270",
"timestamp": "2021-03-24T12:10:36",
"labels": ["61280", "61288", "84273", "117907", "122937", "126160"],
"folderId": "351068",
"schemaTypes": ["therefore", "popular", "commodo", "voluptatem"],
"language": "en",
"countryCode": "US",
"entityType": "informationProfessional"
}
}
""".data(using: .utf8)!
let person = try JSONDecoder().decode(Person.self, from:json2)
print(person)
我认为有两三个问题导致了我认为的崩溃,第一个是缺少元数据,第二个是没有 c_expertise
最后,当 Headshot?
是在 init
中可选,您需要使用 try
而不是 try?
,因为它正在解码一个可选密钥,在这些更改之后:
struct Address: Decodable {
var line1: String
var city: String
var region: String
var postalCode: String
var countryCode: String
}
struct CityCoordinate : Decodable {
var latitude: Double
var longitude: Double
}
struct Thumbnail: Decodable {
var url: String
var width: Int
var height: Int
}
struct Headshot: Decodable {
var url: String
var width: Int
var height: Int
var sourceUrl: String
var thumbnails: [Thumbnail]?
}
struct Meta : Decodable {
var id: String
}
struct Person : Decodable {
var newPatients: Bool?
var address: Address
var description: String?
var name: String
var cityCoordinate: CityCoordinate
var baseURL: String?
var c_baseURL: String?
var headhshot: Headshot?
var insuranceAccepted: [String]?
var expertises: [String]?
var meta: Meta?
enum CodingKeys : String, CodingKey {
case newPatients = "acceptingNewPatients"
case address
case description
case name
case cityCoordinate
case headhshot
case insuranceAccepted
case baseURL = "c_baseURL"
// case expertises = "c_expertises"
case meta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.newPatients = try container.decodeIfPresent(Bool.self, forKey: .newPatients) ?? false
self.address = try container.decode(Address.self, forKey: .address)
self.description = try container.decode(String.self, forKey: .description)
self.name = try container.decode(String.self, forKey: .name)
self.cityCoordinate = try container.decode(CityCoordinate.self, forKey: .cityCoordinate)
self.insuranceAccepted = try? container.decodeIfPresent([String].self, forKey: .insuranceAccepted) ?? []
self.baseURL = try? container.decodeIfPresent(String.self, forKey: .baseURL) ?? ""
self.headhshot = try container.decodeIfPresent(Headshot.self, forKey: .headhshot)
//self.expertises = try? container.decodeIfPresent([String].self, forKey: .expertises) ?? []
self.meta = try container.decodeIfPresent(Meta.self, forKey: .meta)
}
}
let json2 = """
{
"acceptingNewPatients": true,
"address": {
"line1": "111 7th Ave",
"city": "New York",
"region": "NY",
"postalCode": "10005",
"countryCode": "US"
},
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"name": "John Doe",
"cityCoordinate": {
"latitude": 41.78200149536133,
"longitude": -72.83170318603516
},
"c_baseURL": "https://some-url.dummy.com/my-profile/current",
"headshot": {
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"width": 199,
"height": 199,
"sourceUrl": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/199x199.png",
"thumbnails": [{
"url": "https://some-url.dummy.com/my-profile/current/p/ooREDemnhmfahTVexnEyXRC4GTfeKdCDWgUr-GRL11Y/196x196.png",
"width": 196,
"height": 196
}]
},
"insuranceAccepted": ["Finibus", "anything", "hidden", "consectetur" ],
"meta": {
"accountId": "967081498762345229",
"uid": "Nd28v0",
"id": "5119-13270",
"timestamp": "2021-03-24T12:10:36",
"labels": ["61280", "61288", "84273", "117907", "122937", "126160"],
"folderId": "351068",
"schemaTypes": ["therefore", "popular", "commodo", "voluptatem"],
"language": "en",
"countryCode": "US",
"entityType": "informationProfessional"
}
}
""".data(using: .utf8)!
do {
let person = try JSONDecoder().decode(Person.self, from:json2)
print(person)
} catch let error {
print(error)
}
它打印 Person
,请试一试,如果抛出任何错误,请在项目中尝试而不是 Playground
或在线编译器。