如何解开UInt32?在斯威夫特?

时间:2017-04-04 14:44:48

标签: swift unwrap

我正在努力解决一些非常简单的问题,而且我不知道发生了什么。顺便说一下,它是Swift 1.2,但我不认为这是一个特例!

我有一个UInt32?变量,我需要获取该值,但使用!会引发错误:fatal error: unexpectedly found nil while unwrapping an Optional value。不,我知道这不是零。

static func checkBitrate(filePath: String) -> Bool {

    let audioURL = NSURL.fileURLWithPath(filePath)
    var audioFile : AudioFileID = nil
    var theErr: OSStatus? = nil
    let hint: AudioFileTypeID = 0
    theErr = AudioFileOpenURL(audioURL, Int8(kAudioFileReadPermission), hint, &audioFile)

    if (theErr != OSStatus(noErr)) {
        return false
    }

    var outDataSize: UInt32 = 0
    var isWritable: UInt32 = 0
    theErr = AudioFileGetPropertyInfo(audioFile, UInt32(kAudioFilePropertyBitRate), &outDataSize, &isWritable)
    if (theErr != OSStatus(noErr)) {
        return false
    }

    var bitrate: UInt32? = nil
    theErr = AudioFileGetProperty(audioFile, UInt32(kAudioFilePropertyBitRate), &outDataSize, &bitrate)
    if (theErr != OSStatus(noErr)) {
        return false
    }

    println("Bitrate value: \(bitrate)")

    let br = bitrate!
    //return br == 192000
    return false
}

错误在行let br = bitrate!处抛出。

error with value in debugger

我有点迷茫,我做错了什么?

1 个答案:

答案 0 :(得分:1)

您只需要初始化var

 var bitrate: UInt32 = 0