0

我正在为一些非常简单的事情而苦苦挣扎,我不知道发生了什么。顺便说一句,它是 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
}

错误在 line 抛出let br = bitrate!

调试器中的值错误

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

4

1 回答 1

1

你只需要初始化你的 var

 var bitrate: UInt32 = 0 
于 2017-04-04T14:57:05.047 回答