2016-12-18 16 views
1

私は2.3 SWIFT 2.3 SWIFT 2.3がSWIFT 3エラー

8.2 Xcodeで迅速3に迅速に変換する変換:コードがあるコードがあり、コードがあり、コードがあり、コード

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:コードにエラーはありますか?

 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() 
     } 
    } 

後の私のエラーを変換:

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)"

+0

どのラインにエラーが報告されますか? – aircraft

+0

let soundData = tryの場合は?データ(contentsOf:URL(string:fullPath)!) – TuanKenz

+3

'' URL'として既に 'destinationUrl'を持っているなら、なぜ' path'から新しいURLを作成しますか? let soundData = tryの場合はどうすればいいですか?データ(contentsOf:destinationUrl) '。 – rmaddy

答えて

0

エラーの原因は、まず

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

この行である - しかし、問題に関連していない - の種類に注釈を付けていませんコンパイラは推論することができます。 良い非オプション文字列は、悪化暗黙のアンラップオプションです。

プロパティlessonPathは明らかに(暗黙的にアンラップされた)オプションです。文字列補間を使用すると、リテラル"Optional(foo)"が得られます。オプションのラップを解除するか、プロパティがnilの場合はオプションのバインディングを使用する必要があります。値が詳細についてはnil

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

になることはありません場合は非オプションのプロパティを考えてみましょう、これまで初期化子を書くことを避けるために、暗黙的に開封されたoptionals としての性質を宣言決してSwift 3 incorrect string interpolation with implicitly unwrapped Optionals

をお読みください。

+0

ありがとう、私の問題は解決しました – TuanKenz

関連する問題