Convert swift 2.3 to swift 3 error

362 views Asked by At

i convert swift 2.3 to swift 3 in xcode 8.2

swift 2.3: Is there code, Is there code, Is there code, Is there code, Is there code

func playAudio() {
    self.stopAudio()
    let lessonObject:LessonObject = self.lessonArray[self.selectedIndex] as! LessonObject
    let fullPath:String! = Constants.URL_HOST + "\(lessonObject.lessonPath)"
    let soundURL:NSURL! = NSURL.init(string:fullPath)
    let documentsDirectoryURL =  NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
    let destinationUrl = documentsDirectoryURL.URLByAppendingPathComponent(soundURL.lastPathComponent!)
    if NSFileManager().fileExistsAtPath(destinationUrl!.path!) {
        if let soundData = NSData(contentsOfFile: destinationUrl!.path!) {
            self.initAudioWithData(soundData)
        }
        else {
            self.audioErrorAction()
        }
        return
    }
    
    if let soundData = NSData(contentsOfURL:NSURL(string:fullPath)!) {
        self.initAudioWithData(soundData)
    }
    else {
        self.audioErrorAction()
    }
}

swift 3: Is there an error in the code?

        func playAudio() {
        self.stopAudio()
        let lessonObject:LessonObject = self.lessonArray[self.selectedIndex] as! LessonObject
        let fullPath:String! = Constants.URL_HOST + "\(lessonObject.lessonPath)"
let soundURL:URL! = URL.init(fileURLWithPath: fullPath)
        let documentsDirectoryURL =  FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = documentsDirectoryURL.appendingPathComponent(soundURL.lastPathComponent)
        if FileManager().fileExists(atPath: destinationUrl.path){
            if let soundData = try? Data(contentsOf: URL(fileURLWithPath: destinationUrl.path))
            {
                self.initAudioWithData(soundData)
            }
            else {
                self.audioErrorAction()
            }
            return
        }
if let soundData = try? Data(contentsOf: URL(string:fullPath)!)
        {
            self.initAudioWithData(soundData)
        }
        else {
            self.audioErrorAction()
        }
    }

after convert my error:

found nil while unwrapping an Optional value.

I build swift 2.3: destinationUrl = "file:///Users/admin/Library/.../Documents/test.mp3" 0x00006080002a73e0

I build swift 3: destinationUrl = "file:///Users/admin/Library/.../Documents/Optional(%22test.mp3%22)"

1

There are 1 answers

0
vadian On

The source of the error is in this line

let fullPath:String! = Constants.URL_HOST + "\(lessonObject.lessonPath)"

First of all – however not related to the issue – do not annotate types the compiler can infer. You turned a good non-optional string into a worse implicit unwrapped optional.

The property lessonPath is apparently an (implicit unwrapped) optional, too. Using String Interpolation you get literal "Optional(foo)". You need to unwrap the optional or use optional binding if the property could be nil. Consider a non-optional property if the value can never be nil

let fullPath = Constants.URL_HOST + "\(lessonObject.lessonPath!)"

For further information read Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

Never ever declare properties as implicit unwrapped optionals to avoid to write an initializer.