gpt4 book ai didi

ios - Swift 函数无法尝试从 rest 服务获取 JSON 数组并转换为 swift 对象

转载 作者:行者123 更新时间:2023-11-28 13:01:06 26 4
gpt4 key购买 nike

我正在尝试从 REST 端点获取数据并将其转换为一些我可以获得 JSON 的各个属性的对象。

这是我目前的方法:

func getUpcoming(callback: (NSDictionary?, String?) -> Void) {

println("In get upcoming")

var request = NSMutableURLRequest(URL: NSURL(string: Constants.makeUrl("/events")))
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("Bearer 6cfee427-2134-4ba7-89c0-8dbfda2ce6b4", forHTTPHeaderField: "Authorization")

var session = NSURLSession.sharedSession()
var task = session.dataTaskWithRequest(request) {
(data, response, error) -> Void in

println("data = \(data)")
println("response = \(response)")
println("error = \(error)")

if error != nil {
callback(nil, error.localizedDescription)
} else {
var result = NSString(data: data, encoding: NSASCIIStringEncoding)
println("result = \(result)")

let newData: NSData = data
println("before")
var upcomingEvents: NSDictionary = NSJSONSerialization.JSONObjectWithData(newData, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary
println("after") // NEVER GETS HERE

callback(upcomingEvents, nil)
}

}
task.resume()
}

这是一些 println 输出:

...
error = nil
result = [{"id":1912,"name":"Game VS Larson Siding/Kathys Pub","team_id":265,"timezone":"America/Chicago","start":"2015-11-18T20:00:21.000-06:00","end":"2015-11-18T21:00:21.000-06:00","description":null,"location":"Court C","default_response":{"id":1,"label":"No Response"}},{"id":1887,"name":"Lunch Broomball","team_id":29,"timezone":"America/Chicago","start":"2015-11-20T11:15:22.000-06:00","end":"2015-11-20T13:00:22.000-06:00","description":"Be there on the ice at 11:20 for warmup. Games start at 11:30","location":"Graham 4","default_response":{"id":1,"label":"No Response"}},{"id":1892,"name":"Game #5 vs. Cunnilinguys (Goaltender: Zane)","team_id":327,"timezone":"America/Chicago","start":"2015-11-22T10:00:17.000-06:00","end":"2015-11-22T11:00:17.000-06:00","description":null,"location":"Dodge County Ice Arena, Kasson","default_response":{"id":1,"label":"No Response"}}]
before

这是我屏幕上弹出的内容,我从未在我的控制台中收到实际异常。

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x107b98980: pushq %rbp
0x107b98981: movq %rsp, %rbp
0x107b98984: pushq %rbx
0x107b98985: pushq %rax
0x107b98986: movq %rsi, %rcx
0x107b98989: movq %rdi, %rbx
0x107b9898c: xorl %eax, %eax
0x107b9898e: testq %rbx, %rbx
0x107b98991: je 0x107b989ac ; swift_dynamicCastObjCClassUnconditional + 44
0x107b98993: movq 0x7f236(%rip), %rsi ; "isKindOfClass:"
0x107b9899a: movq %rbx, %rdi
0x107b9899d: movq %rcx, %rdx
0x107b989a0: callq 0x107b9b46a ; symbol stub for: objc_msgSend
0x107b989a5: testb %al, %al
0x107b989a7: movq %rbx, %rax
0x107b989aa: je 0x107b989b3 ; swift_dynamicCastObjCClassUnconditional + 51
0x107b989ac: addq $0x8, %rsp
0x107b989b0: popq %rbx
0x107b989b1: popq %rbp
0x107b989b2: retq
0x107b989b3: leaq 0xc158(%rip), %rax ; "Swift dynamic cast failed"
0x107b989ba: movq %rax, 0x87427(%rip) ; gCRAnnotations + 8
0x107b989c1: int3
0x107b989c2: nopw %cs:(%rax,%rax)

我是一名敏捷(和 iOS 开发)新手。请帮忙。

最佳答案

您的结果值是一个数组,如您在方括号中所见。错误是告诉你你在这一行中的类型转换

var upcomingEvents: NSDictionary = NSJSONSerialization.JSONObjectWithData(newData, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary

无法将结果转换为类型 NSDictionary。

您可以通过将结果转换为 NSArray(或 [[String : AnyObject]])并获取数组的第一项来获取您要查找的字典。

关于ios - Swift 函数无法尝试从 rest 服务获取 JSON 数组并转换为 swift 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33791340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com