Swift动态演员失败

时间:2015-03-09 16:19:44

标签: json swift xcode6

我正在尝试获取用户的ID并且我的脚本仍然失败:

    func getUserID(users:NSString){
    var username:NSString = "abcd"
    println(username)
    if ( username.isEqualToString("")) {

        var alertView:UIAlertView = UIAlertView()
        alertView.title = "Failed to find user"
        alertView.message = "Please try logging in and out."
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else {

    var post:NSString = "username=\(username)"

    NSLog("PostData: %@",post);

    var url:NSURL = NSURL(string: "http://turbo-tires-21-188007.euw1.nitrousbox.com/jsonGetUserID.php")!

    var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

    var postLength:NSString = String( postData.length )

    var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.HTTPBody = postData
    request.setValue(postLength, forHTTPHeaderField: "Content-Length")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")


    var reponseError: NSError?
    var response: NSURLResponse?

    var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

    if ( urlData != nil ) {
        let res = response as NSHTTPURLResponse!;

        NSLog("Response code: %ld", res.statusCode);

        if (res.statusCode >= 200 && res.statusCode < 300)
        {
            var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

            NSLog("Response ==> %@", responseData);

            var error: NSError?

            let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary


            let idusers:NSInteger = jsonData.valueForKey("idusers") as NSInteger

        }
    }
    }

}

,错误输出如下:

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x111199620:  pushq  %rbp
0x111199621:  movq   %rsp, %rbp
0x111199624:  pushq  %rbx
0x111199625:  pushq  %rax
0x111199626:  movq   %rsi, %rcx
0x111199629:  movq   %rdi, %rbx
0x11119962c:  xorl   %eax, %eax
0x11119962e:  testq  %rbx, %rbx
0x111199631:  je     0x11119964c               ;
swift_dynamicCastObjCClassUnconditional + 44
0x111199633:  movq   0x82756(%rip), %rsi       ; "isKindOfClass:"
0x11119963a:  movq   %rbx, %rdi
0x11119963d:  movq   %rcx, %rdx
0x111199640:  callq  0x11119c1ca               ; symbol stub for:  
objc_msgSend
0x111199645:  testb  %al, %al
0x111199647:  movq   %rbx, %rax
0x11119964a:  je     0x111199653               ; 
swift_dynamicCastObjCClassUnconditional + 51
0x11119964c:  addq   $0x8, %rsp
0x111199650:  popq   %rbx
0x111199651:  popq   %rbp
0x111199652:  retq   
0x111199653:  leaq   0xcdc8(%rip), %rax        ; "Swift dynamic cast failed"
0x11119965a:  movq   %rax, 0x8ae57(%rip)       ; gCRAnnotations + 8
0x111199661:  int3   
0x111199662:  nopw   %cs:(%rax,%rax)

我已经尝试了很多不同的方式,认为这是字符串的格式,但到目前为止还没有任何工作。

getUserID("abcd");

这就是我调用方法的方法。此错误导致应用程序崩溃,并从上面的错误中得出结论,我得到(lldb)安慰。任何帮助都会很棒。

更新:我缩小了错误:

let idusers:NSInteger = jsonData.valueForKey("idusers") as NSInteger

解决: 问题是json中的数字是一个字符串所以我使用NSString并且它可以工作。

1 个答案:

答案 0 :(得分:1)

由于jsonData是NSDictionary,因此它只能包含对象。 NSInteger不是一个对象,它是intlong的类型转换。我假设你的价值实际上是一个NSNumber,所以你的演员应该是:

let idusers: = jsonData.valueForKey("idusers") as NSNumber

在您的情况下,值结果是一个字符串:

let idusers: = jsonData.valueForKey("idusers") as String