(SWIFT)(String,Any)不能转换为String

时间:2014-11-29 18:10:00

标签: swift xcode6.1

我正在使用swift来监控我的电池信息但是我从这行代码(String,Any) id not convertible to String获得了binfo.stringValue = x.battery["DesignCapacity"],其中binfo是一个GUI标签。

以下是完整代码:

import Cocoa
func executeCommand(command: String, args: [String]) -> String {

    let task = NSTask()

    task.launchPath = command
    task.arguments = args

    let pipe = NSPipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)!

    return output

}

extension Array {
    func combine(separator: String) -> String{
        var str : String = ""
        for (idx, item) in enumerate(self) {
            str += "\(item)"
            if idx < self.count-1 {
                str += separator
            }
        }
        return str
    }
}

extension String{
    func replace(x:String,y:String) -> String{
        var z = self.stringByReplacingOccurrencesOfString(x, withString: y, options: NSStringCompareOptions.LiteralSearch, range: nil)
        return z
    }
}


extension String{
    func split(delimiter:String) -> Array<String>{
        return self.componentsSeparatedByString(delimiter)
    }
}




func get_key_and_value(string:String) -> Array<Any>{
    var x = string.split(" = ")
    if x[1].toInt() != nil{
        return [x[0],x[1].toInt()]
    }  else if x[1] == "Yes"{
        return [x[0],true]
    } else if x[1] == "No"{
        return [x[0],false]
    } else {
        return [x[0],x[1]]
    }
    //return [return_val[0],return_val[1]]
}

class Battery{
    var battery = Dictionary<String, Any>()
    init(){
        refreshValues()
    }
    internal func refreshValues(){
        let commandOutput:String = executeCommand("/usr/sbin/ioreg", ["-r","-w0","-cAppleSmartBattery"])
        var commandOutList = commandOutput.split("\n")

        commandOutList.removeAtIndex(0)
        commandOutList.removeAtIndex(0)

        for _ in 1...5{
            var lastRemoved = commandOutList.removeLast()
            if lastRemoved == "}"{
                break
            }
        }

        //Got Needed Infomation Only

        let commandOutStr = commandOutList.combine("")
        let cmdOutStr = commandOutStr.replace("\"",y: "").replace("    ",y: "\n")

        var cmdOutSplit = cmdOutStr.split("\n")
        cmdOutSplit.removeAtIndex(0)


        //var x = get_key_and_value(cmdOutSplit[0])
        //var key:String = x[0] as String
        //var value = x[1] as Any

        for x in cmdOutSplit{
            var y = get_key_and_value(x)
            battery[(y[0] as String).replace(" ", y: "")] = y[1] as Any
        }
    }
}

class ViewController: NSViewController {
    @IBOutlet weak var binfo: NSTextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        var x = Battery()
        println(x.battery["DesignCapacity"])
        binfo.stringValue = x.battery["DesignCapacity"]
        // Do any additional setup after loading the view.
    }



    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

但是,如果我将binfo.stringValue = x.battery["DesignCapacity"]更改为binfo.stringValue = x.battery["DesignCapacity"] as String,我会收到"Swift dynamic cast failure"错误,其日志为

libswiftCore.dylib`swift_dynamicCast:
0x1001b52f0:  pushq  %rbp
0x1001b52f1:  movq   %rsp, %rbp
0x1001b52f4:  pushq  %r15
0x1001b52f6:  pushq  %r14
0x1001b52f8:  pushq  %r13
0x1001b52fa:  pushq  %r12
0x1001b52fc:  pushq  %rbx
0x1001b52fd:  subq   $0x58, %rsp
0x1001b5301:  movq   %rcx, %r13
0x1001b5304:  movq   %rdx, %rbx
0x1001b5307:  movq   %rsi, %rdx
0x1001b530a:  movq   %rdi, %r12
0x1001b530d:  movl   $0x80000000, %r14d
0x1001b5313:  movq   (%r13), %rcx
0x1001b5317:  xorl   %eax, %eax
0x1001b5319:  cmpq   $0x41, %rcx
0x1001b531d:  cmovbeq %rcx, %rax
0x1001b5321:  cmpq   $0x3f, %rax
0x1001b5325:  jg     0x1001b5388               ; swift_dynamicCast + 152
0x1001b5327:  cmpq   $0x11, %rax
0x1001b532b:  ja     0x1001b55b0               ; swift_dynamicCast + 704
0x1001b5331:  leaq   0x504(%rip), %rcx         ; swift_dynamicCast + 1356
0x1001b5338:  movslq (%rcx,%rax,4), %rax
0x1001b533c:  addq   %rcx, %rax
0x1001b533f:  jmpq   *%rax
0x1001b5341:  movq   (%rbx), %rcx
0x1001b5344:  xorl   %eax, %eax
0x1001b5346:  cmpq   $0x41, %rcx
0x1001b534a:  cmovbeq %rcx, %rax
0x1001b534e:  cmpq   $0x3f, %rax
0x1001b5352:  jg     0x1001b5542               ; swift_dynamicCast + 594
0x1001b5358:  cmpq   $0x11, %rax
0x1001b535c:  ja     0x1001b55b0               ; swift_dynamicCast + 704
0x1001b5362:  leaq   0x51b(%rip), %rcx         ; swift_dynamicCast + 1428
0x1001b5369:  movslq (%rcx,%rax,4), %rax
0x1001b536d:  addq   %rcx, %rax
0x1001b5370:  jmpq   *%rax
0x1001b5372:  movq   (%rdx), %rsi
0x1001b5375:  movq   %r12, %rdi
0x1001b5378:  movq   %r13, %rdx
0x1001b537b:  movq   %r8, %rcx
0x1001b537e:  callq  0x1001b58d0               ; _dynamicCastUnknownClass(swift::OpaqueValue*, void*, swift::Metadata const*, swift::DynamicCastFlags)
0x1001b5383:  jmp    0x1001b55a8               ; swift_dynamicCast + 696
0x1001b5388:  addq   $-0x40, %rax
0x1001b538c:  cmpq   $0x2, %rax
0x1001b5390:  jae    0x1001b55b0               ; swift_dynamicCast + 704
0x1001b5396:  cmpq   %r13, %rbx
0x1001b5399:  je     0x1001b53fa               ; swift_dynamicCast + 266
0x1001b539b:  cmpq   $0xc, (%rbx)
0x1001b539f:  jne    0x1001b5418               ; swift_dynamicCast + 296
0x1001b53a1:  testq  %rbx, %rbx
0x1001b53a4:  je     0x1001b5418               ; swift_dynamicCast + 296
0x1001b53a6:  movq   %r12, %r15
0x1001b53a9:  movq   %r8, %r12
0x1001b53ac:  testq  0x8(%rbx), %r14
0x1001b53b0:  je     0x1001b543f               ; swift_dynamicCast + 335
0x1001b53b6:  movq   0x18(%rdx), %rbx
0x1001b53ba:  movq   -0x8(%rbx), %rax
0x1001b53be:  movq   %rdx, %rdi
0x1001b53c1:  movq   %rbx, %rsi
0x1001b53c4:  movq   %rdx, -0x58(%rbp)
0x1001b53c8:  callq  *0x10(%rax)
0x1001b53cb:  movq   %rax, %r14
0x1001b53ce:  movq   %r15, %rdi
0x1001b53d1:  movq   %r14, %rsi
0x1001b53d4:  movq   %rbx, %rdx
0x1001b53d7:  movq   %r13, %rcx
0x1001b53da:  movq   %r12, %rbx
0x1001b53dd:  movq   %rbx, %r8
0x1001b53e0:  callq  0x1001b52f0               ; swift_dynamicCast
0x1001b53e5:  movq   -0x58(%rbp), %rdi
0x1001b53e9:  movb   %al, %r12b
0x1001b53ec:  cmpq   %rdi, %r14
0x1001b53ef:  jne    0x1001b56b9               ; swift_dynamicCast + 969
0x1001b53f5:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b53fa:  testb  $0x2, %r8b
0x1001b53fe:  movq   -0x8(%r13), %rax
0x1001b5402:  movq   %r12, %rdi
0x1001b5405:  movq   %rdx, %rsi
0x1001b5408:  movq   %r13, %rdx
0x1001b540b:  je     0x1001b5434               ; swift_dynamicCast + 324
0x1001b540d:  callq  *0x48(%rax)
0x1001b5410:  movb   $0x1, %r12b
0x1001b5413:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b5418:  testb  $0x1, %r8b
0x1001b541c:  jne    0x1001b57ee               ; swift_dynamicCast + 1278
0x1001b5422:  testb  $0x4, %r8b
0x1001b5426:  jne    0x1001b555c               ; swift_dynamicCast + 620
0x1001b542c:  xorl   %r12d, %r12d
0x1001b542f:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b5434:  callq  *0x30(%rax)
0x1001b5437:  movb   $0x1, %r12b
0x1001b543a:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b543f:  movq   (%rdx), %rdi
0x1001b5442:  movq   %rdx, %rbx
0x1001b5445:  callq  0x1001deab0               ; swift_getObjectType
0x1001b544a:  movq   %r15, %rdi
0x1001b544d:  movq   %rbx, %rsi
0x1001b5450:  movq   %rax, %rdx
0x1001b5453:  movq   %r13, %rcx
0x1001b5456:  movq   %r12, %r8
0x1001b5459:  callq  0x1001b52f0               ; swift_dynamicCast
0x1001b545e:  jmp    0x1001b55a8               ; swift_dynamicCast + 696
0x1001b5463:  movq   (%rbx), %rcx
0x1001b5466:  xorl   %eax, %eax
0x1001b5468:  cmpq   $0x41, %rcx
0x1001b546c:  cmovbeq %rcx, %rax
0x1001b5470:  cmpq   $0x10, %rax
0x1001b5474:  ja     0x1001b5396               ; swift_dynamicCast + 166
0x1001b547a:  movl   $0x14001, %ecx
0x1001b547f:  btq    %rax, %rcx
0x1001b5483:  jae    0x1001b5396               ; swift_dynamicCast + 166
0x1001b5489:  leaq   0x44760(%rip), %rsi       ; _TMpSs21_ObjectiveCBridgeable
0x1001b5490:  movq   %r13, %rdi
0x1001b5493:  movq   %r12, -0x58(%rbp)
0x1001b5497:  movq   %r8, %r12
0x1001b549a:  movq   %rdx, %r15
0x1001b549d:  callq  0x1001b6b60               ; swift_conformsToProtocol
0x1001b54a2:  movq   %r15, %rdx
0x1001b54a5:  movq   %r12, %r8
0x1001b54a8:  movq   -0x58(%rbp), %r12
0x1001b54ac:  testq  %rax, %rax
0x1001b54af:  je     0x1001b5396               ; swift_dynamicCast + 166
0x1001b54b5:  movq   %rdx, %r15
0x1001b54b8:  movq   %r8, -0x60(%rbp)
0x1001b54bc:  movq   %r13, %rdi
0x1001b54bf:  movq   %r13, %rsi
0x1001b54c2:  movq   %rax, %r14
0x1001b54c5:  callq  *0x8(%r14)
0x1001b54c9:  testb  %al, %al
0x1001b54cb:  je     0x1001b55c7               ; swift_dynamicCast + 727
0x1001b54d1:  movq   %r13, %rdi
0x1001b54d4:  movq   %r13, %rsi
0x1001b54d7:  callq  *0x10(%r14)
0x1001b54db:  movq   %r14, -0x68(%rbp)
0x1001b54df:  movq   (%r15), %rsi
0x1001b54e2:  movq   $0x0, -0x30(%rbp)
0x1001b54ea:  leaq   -0x30(%rbp), %rdi
0x1001b54ee:  movq   %rax, %rdx
0x1001b54f1:  movq   -0x60(%rbp), %rbx
0x1001b54f5:  movq   %rbx, %rcx
0x1001b54f8:  callq  0x1001b58d0               ; _dynamicCastUnknownClass(swift::OpaqueValue*, void*, swift::Metadata const*, swift::DynamicCastFlags)
0x1001b54fd:  testb  %al, %al
0x1001b54ff:  je     0x1001b566b               ; swift_dynamicCast + 891
0x1001b5505:  movq   %rbx, %rax
0x1001b5508:  movq   %rbx, %r14
0x1001b550b:  andq   $0x6, %rax
0x1001b550f:  cmpq   $0x6, %rax
0x1001b5513:  movb   $0x1, %al
0x1001b5515:  je     0x1001b5522               ; swift_dynamicCast + 562
0x1001b5517:  movq   -0x30(%rbp), %rdi
0x1001b551b:  callq  0x1001ddcd0               ; swift_unknownRetain
0x1001b5520:  xorl   %eax, %eax
0x1001b5522:  movl   %eax, -0x6c(%rbp)
0x1001b5525:  movq   -0x8(%r13), %rcx
0x1001b5529:  xorl   %eax, %eax
0x1001b552b:  cmpq   $0x19, 0x98(%rcx)
0x1001b5533:  jae    0x1001b5726               ; swift_dynamicCast + 1078
0x1001b5539:  leaq   -0x50(%rbp), %r15
0x1001b553d:  jmp    0x1001b5735               ; swift_dynamicCast + 1093
0x1001b5542:  addq   $-0x40, %rax
0x1001b5546:  cmpq   $0x2, %rax
0x1001b554a:  jae    0x1001b55b0               ; swift_dynamicCast + 704
0x1001b554c:  testb  $0x1, %r8b
0x1001b5550:  jne    0x1001b57fd               ; swift_dynamicCast + 1293
0x1001b5556:  testb  $0x4, %r8b
0x1001b555a:  je     0x1001b55bf               ; swift_dynamicCast + 719
0x1001b555c:  movq   -0x8(%rbx), %rax
0x1001b5560:  movq   %rdx, %rdi
0x1001b5563:  movq   %rbx, %rsi
0x1001b5566:  callq  *0x20(%rax)
0x1001b5569:  xorl   %r12d, %r12d
0x1001b556c:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b5571:  movq   %r12, %rdi
0x1001b5574:  movq   %rdx, %rsi
0x1001b5577:  movq   %rbx, %rdx
0x1001b557a:  movq   %r13, %rcx
0x1001b557d:  callq  0x1001b5b20               ; _dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::Metadata const*, swift::ExistentialTypeMetadata const*, swift::DynamicCastFlags)
0x1001b5582:  jmp    0x1001b55a8               ; swift_dynamicCast + 696
0x1001b5584:  movq   %r12, %rdi
0x1001b5587:  movq   %rdx, %rsi
0x1001b558a:  movq   %rbx, %rdx
0x1001b558d:  movq   %r13, %rcx
0x1001b5590:  callq  0x1001b5ec0               ; _dynamicCastToMetatype(swift::OpaqueValue*, swift::OpaqueValue*, swift::Metadata const*, swift::MetatypeMetadata const*, swift::DynamicCastFlags)
0x1001b5595:  jmp    0x1001b55a8               ; swift_dynamicCast + 696
0x1001b5597:  movq   %r12, %rdi
0x1001b559a:  movq   %rdx, %rsi
0x1001b559d:  movq   %rbx, %rdx
0x1001b55a0:  movq   %r13, %rcx
0x1001b55a3:  callq  0x1001b6600               ; _dynamicCastToExistentialMetatype(swift::OpaqueValue*, swift::OpaqueValue*, swift::Metadata const*, swift::ExistentialMetatypeMetadata const*, swift::DynamicCastFlags)
0x1001b55a8:  movb   %al, %r12b
0x1001b55ab:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b55b0:  leaq   0x363ad(%rip), %rax       ; "Corrupt Swift type object"
0x1001b55b7:  movq   %rax, 0x5ae9a(%rip)       ; gCRAnnotations + 8
0x1001b55be:  int3   
0x1001b55bf:  xorl   %r12d, %r12d
0x1001b55c2:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b55c7:  movq   -0x60(%rbp), %r14
0x1001b55cb:  testb  $0x1, %r14b
0x1001b55cf:  jne    0x1001b580c               ; swift_dynamicCast + 1308
0x1001b55d5:  testb  $0x4, %r14b
0x1001b55d9:  movq   %r15, %rdi
0x1001b55dc:  jne    0x1001b571d               ; swift_dynamicCast + 1069
0x1001b55e2:  xorl   %r12d, %r12d
0x1001b55e5:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b55ea:  movq   %rdx, -0x58(%rbp)
0x1001b55ee:  movq   %r8, %r14
0x1001b55f1:  leaq   0x445f8(%rip), %rsi       ; _TMpSs21_ObjectiveCBridgeable
0x1001b55f8:  movq   %rbx, %rdi
0x1001b55fb:  callq  0x1001b6b60               ; swift_conformsToProtocol
0x1001b5600:  testq  %rax, %rax
0x1001b5603:  je     0x1001b56ec               ; swift_dynamicCast + 1020
0x1001b5609:  movq   %rbx, %rdi
0x1001b560c:  movq   %rbx, %rsi
0x1001b560f:  movq   %rax, %r15
0x1001b5612:  callq  *0x8(%r15)
0x1001b5616:  movq   %r15, %rcx
0x1001b5619:  testb  %al, %al
0x1001b561b:  je     0x1001b5705               ; swift_dynamicCast + 1045
0x1001b5621:  movq   -0x58(%rbp), %r15
0x1001b5625:  movq   %r15, %rdi
0x1001b5628:  movq   %rbx, %rsi
0x1001b562b:  callq  *0x18(%rcx)
0x1001b562e:  movq   %r14, %rcx
0x1001b5631:  orq    $0x6, %rcx
0x1001b5635:  movq   %r12, %rdi
0x1001b5638:  movq   %rax, %rsi
0x1001b563b:  movq   %r13, %rdx
0x1001b563e:  callq  0x1001b58d0               ; _dynamicCastUnknownClass(swift::OpaqueValue*, void*, swift::Metadata const*, swift::DynamicCastFlags)
0x1001b5643:  movb   %al, %r12b
0x1001b5646:  testb  %r12b, %r12b
0x1001b5649:  sete   %al
0x1001b564c:  movzbl %al, %eax
0x1001b564f:  imulq  $0x2, %rax, %rax
0x1001b5653:  addq   $0x2, %rax
0x1001b5657:  testq  %r14, %rax
0x1001b565a:  je     0x1001b56da               ; swift_dynamicCast + 1002
0x1001b565c:  movq   -0x8(%rbx), %rax
0x1001b5660:  movq   %r15, %rdi
0x1001b5663:  movq   %rbx, %rsi
0x1001b5666:  callq  *0x20(%rax)
0x1001b5669:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b566b:  xorl   %r12d, %r12d
0x1001b566e:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b5670:  movq   %r12, %r15
0x1001b5673:  movq   %r8, %r12
0x1001b5676:  testq  0x8(%rbx), %r14
0x1001b567a:  je     0x1001b579a               ; swift_dynamicCast + 1194
0x1001b5680:  movq   0x18(%rdx), %rbx
0x1001b5684:  movq   -0x8(%rbx), %rax
0x1001b5688:  movq   %rdx, %rdi
0x1001b568b:  movq   %rbx, %rsi
0x1001b568e:  movq   %rdx, %r14
0x1001b5691:  callq  *0x10(%rax)
0x1001b5694:  movq   %r15, %rdi
0x1001b5697:  movq   %rax, %rsi
0x1001b569a:  movq   %rax, %r15
0x1001b569d:  movq   %rbx, %rdx
0x1001b56a0:  movq   %r13, %rcx
0x1001b56a3:  movq   %r12, %rbx
0x1001b56a6:  movq   %rbx, %r8
0x1001b56a9:  callq  0x1001b52f0               ; swift_dynamicCast
0x1001b56ae:  movq   %r14, %rdi
0x1001b56b1:  movb   %al, %r12b
0x1001b56b4:  cmpq   %rdi, %r15
0x1001b56b7:  je     0x1001b56da               ; swift_dynamicCast + 1002
0x1001b56b9:  testb  %r12b, %r12b
0x1001b56bc:  sete   %al
0x1001b56bf:  movzbl %al, %eax
0x1001b56c2:  imulq  $0x2, %rax, %rax
0x1001b56c6:  addq   $0x2, %rax
0x1001b56ca:  testq  %rbx, %rax
0x1001b56cd:  je     0x1001b56da               ; swift_dynamicCast + 1002
0x1001b56cf:  movq   0x18(%rdi), %rsi
0x1001b56d3:  movq   -0x8(%rsi), %rax
0x1001b56d7:  callq  *0x18(%rax)
0x1001b56da:  movb   %r12b, %al
0x1001b56dd:  addq   $0x58, %rsp
0x1001b56e1:  popq   %rbx
0x1001b56e2:  popq   %r12
0x1001b56e4:  popq   %r13
0x1001b56e6:  popq   %r14
0x1001b56e8:  popq   %r15
0x1001b56ea:  popq   %rbp
0x1001b56eb:  retq   
0x1001b56ec:  testb  $0x1, %r14b
0x1001b56f0:  movq   -0x58(%rbp), %rdi
0x1001b56f4:  jne    0x1001b581b               ; swift_dynamicCast + 1323
0x1001b56fa:  testb  $0x4, %r14b
0x1001b56fe:  jne    0x1001b571d               ; swift_dynamicCast + 1069
0x1001b5700:  xorl   %r12d, %r12d
0x1001b5703:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b5705:  testb  $0x1, %r14b
0x1001b5709:  jne    0x1001b582a               ; swift_dynamicCast + 1338
0x1001b570f:  testb  $0x4, %r14b
0x1001b5713:  movq   -0x58(%rbp), %rdi
0x1001b5717:  je     0x1001b57b0               ; swift_dynamicCast + 1216
0x1001b571d:  movq   -0x8(%rbx), %rax
0x1001b5721:  jmp    0x1001b5563               ; swift_dynamicCast + 627
0x1001b5726:  movq   0x88(%rcx), %rdi
0x1001b572d:  callq  0x1001e177c               ; symbol stub for: malloc
0x1001b5732:  movq   %rax, %r15
0x1001b5735:  movq   %rax, -0x78(%rbp)
0x1001b5739:  xorl   %edx, %edx
0x1001b573b:  movl   $0x1, %ecx
0x1001b5740:  movq   %r15, %rdi
0x1001b5743:  movq   %r13, %rsi
0x1001b5746:  callq  0x1001df760               ; swift_storeEnumTagSinglePayload
0x1001b574b:  testb  $0x1, %r14b
0x1001b574f:  je     0x1001b576a               ; swift_dynamicCast + 1146
0x1001b5751:  movq   -0x30(%rbp), %rbx
0x1001b5755:  movq   %rbx, %rdi
0x1001b5758:  movq   %r15, %rsi
0x1001b575b:  movq   %r13, %rdx
0x1001b575e:  movq   %r13, %rcx
0x1001b5761:  movq   -0x68(%rbp), %rax
0x1001b5765:  callq  *0x20(%rax)
0x1001b5768:  jmp    0x1001b5785               ; swift_dynamicCast + 1173
0x1001b576a:  movq   -0x30(%rbp), %rbx
0x1001b576e:  movq   %rbx, %rdi
0x1001b5771:  movq   %r15, %rsi
0x1001b5774:  movq   %r13, %rdx
0x1001b5777:  movq   %r13, %rcx
0x1001b577a:  movq   -0x68(%rbp), %rax
0x1001b577e:  callq  *0x28(%rax)
0x1001b5781:  testb  %al, %al
0x1001b5783:  je     0x1001b57b8               ; swift_dynamicCast + 1224
0x1001b5785:  movq   -0x8(%r13), %rax
0x1001b5789:  movq   %r12, %rdi
0x1001b578c:  movq   %r15, %rsi
0x1001b578f:  movq   %r13, %rdx
0x1001b5792:  callq  *0x48(%rax)
0x1001b5795:  movb   $0x1, %r12b
0x1001b5798:  jmp    0x1001b57bb               ; swift_dynamicCast + 1227
0x1001b579a:  movq   (%rdx), %rsi
0x1001b579d:  movq   %r15, %rdi
0x1001b57a0:  movq   %r13, %rdx
0x1001b57a3:  movq   %r12, %rcx
0x1001b57a6:  callq  0x1001b58d0               ; _dynamicCastUnknownClass(swift::OpaqueValue*, void*, swift::Metadata const*, swift::DynamicCastFlags)
0x1001b57ab:  jmp    0x1001b55a8               ; swift_dynamicCast + 696
0x1001b57b0:  xorl   %r12d, %r12d
0x1001b57b3:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b57b8:  xorl   %r12d, %r12d
0x1001b57bb:  movl   -0x6c(%rbp), %eax
0x1001b57be:  testb  %al, %al
0x1001b57c0:  jne    0x1001b57e0               ; swift_dynamicCast + 1264
0x1001b57c2:  testb  %r12b, %r12b
0x1001b57c5:  sete   %al
0x1001b57c8:  movzbl %al, %eax
0x1001b57cb:  imulq  $0x2, %rax, %rax
0x1001b57cf:  addq   $0x2, %rax
0x1001b57d3:  testq  %r14, %rax
0x1001b57d6:  je     0x1001b57e0               ; swift_dynamicCast + 1264
0x1001b57d8:  movq   %rbx, %rdi
0x1001b57db:  callq  0x1001ddd20               ; swift_unknownRelease
0x1001b57e0:  movq   -0x78(%rbp), %rdi
0x1001b57e4:  callq  0x1001e1752               ; symbol stub for: free
0x1001b57e9:  jmp    0x1001b56da               ; swift_dynamicCast + 1002
0x1001b57ee:  leaq   0x36189(%rip), %rax       ; "Swift dynamic cast failure"
0x1001b57f5:  movq   %rax, 0x5ac5c(%rip)       ; gCRAnnotations + 8
0x1001b57fc:  int3   
0x1001b57fd:  leaq   0x3617a(%rip), %rax       ; "Swift dynamic cast failure"
0x1001b5804:  movq   %rax, 0x5ac4d(%rip)       ; gCRAnnotations + 8
0x1001b580b:  int3   
0x1001b580c:  leaq   0x3616b(%rip), %rax       ; "Swift dynamic cast failure"
0x1001b5813:  movq   %rax, 0x5ac3e(%rip)       ; gCRAnnotations + 8
0x1001b581a:  int3   
0x1001b581b:  leaq   0x3615c(%rip), %rax       ; "Swift dynamic cast failure"
0x1001b5822:  movq   %rax, 0x5ac2f(%rip)       ; gCRAnnotations + 8
0x1001b5829:  int3   
0x1001b582a:  leaq   0x3614d(%rip), %rax       ; "Swift dynamic cast failure"
0x1001b5831:  movq   %rax, 0x5ac20(%rip)       ; gCRAnnotations + 8
0x1001b5838:  int3   
0x1001b5839:  nopl   (%rax)
0x1001b583c:  addl   $0x27fffffb, %eax
0x1001b5841:  cld

如果有人能提供帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:2)

您的代码应该运行一些更改;

将int值添加到字典中时,您需要添加可选的Int。而是使用!运算符添加实际的Int;

if x[1].toInt() != nil{
    return [x[0],x[1].toInt()!]
}  else if x[1] == "Yes"{

然后(如你所知,它是字典中的Int)你可以使用String构造函数将它转换为字符串;

binfo.stringValue = String(x.battery["DesignCapacity"] as Int)