я конвертирую swift 2.3 в swift 3 в xcode 8.2
swift 2.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()
}
}
после преобразования моей ошибки:
найдено nil при развертывании необязательного значения.
Я строю swift 2.3: destinationUrl = file:///Users/admin/Library/.../Documents/test.mp3 0x00006080002a73e0
Я создаю swift 3: destinationUrl = file:///Users/admin/Library/.../Documents/Optional(%22test.mp3%22)
destinationUrl
какURL
, зачем вы создаете новый URL из егоpath
? Просто сделайтеif let soundData = try? Data(contentsOf: destinationUrl)
. - person rmaddy   schedule 18.12.2016fullPath
? - person vadian   schedule 18.12.2016fullPath
недопустимая строка URL-адреса, можете ли вы вставить ее сюда? И в будущем старайтесь избегать использования!
. - person user28434'mstep   schedule 19.12.2016init(fileURLWithPath: String)
? вместоinit(string: String)
? - person Sulthan   schedule 19.12.2016soundURL
вместо того, чтобы дважды преобразовывать путь в URL? - person user28434'mstep   schedule 19.12.2016