Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

多个URL的NSFileManager Swift数组

在这种情况下,我认为您应该使用NSURLSession.sharedSession()。dataTaskWithURL从链接中进行多次下载,但要保持下载 *** 作异步。您需要向其添加一个回调块。您应该执行以下 *** 作:

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask).first as NSURLlet musicArray:[String] = ["http://freetone.org/ring/stan/iPhone_5-Alarm.mp3","http://freetone.org/ring/stan2/Samsung_Galaxy_S4-SMS.mp3","https://www.sounddogs.com/sound-effects/25/mp3/235178_SOUNDDOGS__al.mp3"]var musicUrls:[NSURL!]!// create a function to start the audio data downloadfunc getAudioDataFromUrl(audioUrl:NSURL, completion: ((data: NSData?) -> Void)) {    NSURLSession.sharedSession().dataTaskWithURL(audioUrl) { (data, response, error) in        completion(data:  data)    }.resume()}// create another function to save the audio datafunc saveAudioData(audio:NSData, destination:NSURL) -> Bool {    if audio.writeToURL(destination, atomically: true) {        println("The file "(destination.lastPathComponent!.stringByDeletingPathExtension)" was successfully saved.")        return true    }    return false}// just convert your links to Urlsfunc linksToUrls(){    musicUrls = musicArray        .map() { NSURL(string: [+++]) }        .filter() { [+++] != nil }}// create a loop to start downloading your urlsfunc startDownloadingUrls(){    for url in musicUrls {        let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!)        if NSFileManager().fileExistsAtPath(destinationUrl.path!) { println("The file "(destinationUrl.lastPathComponent!.stringByDeletingPathExtension)" already exists at path.")        } else { println("Started downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".") getAudioDataFromUrl(url) { data in     dispatch_async(dispatch_get_main_queue()) {         println("Finished downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".")         println("Started saving "(url.lastPathComponent!.stringByDeletingPathExtension)".")         if self.saveAudioData(data!, destination: self.documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!) ) {  // do what ever if writeToURL was successful         } else {  println("The File "(url.lastPathComponent!.stringByDeletingPathExtension)" was not saved.")         }     } }        }    }}override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.    println("Begin of pre")    linksToUrls()    startDownloadingUrls()    println("End of pre")}


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 2, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

多个URL的NSFileManager Swift数组

在这种情况下,我认为您应该使用NSURLSession.sharedSession()。dataTaskWithURL从链接中进行多次下载,但要保持下载 *** 作异步。您需要向其添加一个回调块。您应该执行以下 *** 作:

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask).first as NSURLlet musicArray:[String] = ["http://freetone.org/ring/stan/iPhone_5-Alarm.mp3","http://freetone.org/ring/stan2/Samsung_Galaxy_S4-SMS.mp3","https://www.sounddogs.com/sound-effects/25/mp3/235178_SOUNDDOGS__al.mp3"]var musicUrls:[NSURL!]!// create a function to start the audio data downloadfunc getAudioDataFromUrl(audioUrl:NSURL, completion: ((data: NSData?) -> Void)) {    NSURLSession.sharedSession().dataTaskWithURL(audioUrl) { (data, response, error) in        completion(data:  data)    }.resume()}// create another function to save the audio datafunc saveAudioData(audio:NSData, destination:NSURL) -> Bool {    if audio.writeToURL(destination, atomically: true) {        println("The file "(destination.lastPathComponent!.stringByDeletingPathExtension)" was successfully saved.")        return true    }    return false}// just convert your links to Urlsfunc linksToUrls(){    musicUrls = musicArray        .map() { NSURL(string: ) }        .filter() { [+++] != nil }}// create a loop to start downloading your urlsfunc startDownloadingUrls(){    for url in musicUrls {        let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!)        if NSFileManager().fileExistsAtPath(destinationUrl.path!) { println("The file "(destinationUrl.lastPathComponent!.stringByDeletingPathExtension)" already exists at path.")        } else { println("Started downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".") getAudioDataFromUrl(url) { data in     dispatch_async(dispatch_get_main_queue()) {         println("Finished downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".")         println("Started saving "(url.lastPathComponent!.stringByDeletingPathExtension)".")         if self.saveAudioData(data!, destination: self.documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!) ) {  // do what ever if writeToURL was successful         } else {  println("The File "(url.lastPathComponent!.stringByDeletingPathExtension)" was not saved.")         }     } }        }    }}override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.    println("Begin of pre")    linksToUrls()    startDownloadingUrls()    println("End of pre")}


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
多个URL的NSFileManager Swift数组_随笔_内存溢出

多个URL的NSFileManager Swift数组

多个URL的NSFileManager Swift数组,第1张

多个URL的NSFileManager Swift数组

在这种情况下,我认为您应该使用NSURLSession.sharedSession()。dataTaskWithURL从链接中进行多次下载,但要保持下载 *** 作异步。您需要向其添加一个回调块。您应该执行以下 *** 作:

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask).first as NSURLlet musicArray:[String] = ["http://freetone.org/ring/stan/iPhone_5-Alarm.mp3","http://freetone.org/ring/stan2/Samsung_Galaxy_S4-SMS.mp3","https://www.sounddogs.com/sound-effects/25/mp3/235178_SOUNDDOGS__al.mp3"]var musicUrls:[NSURL!]!// create a function to start the audio data downloadfunc getAudioDataFromUrl(audioUrl:NSURL, completion: ((data: NSData?) -> Void)) {    NSURLSession.sharedSession().dataTaskWithURL(audioUrl) { (data, response, error) in        completion(data:  data)    }.resume()}// create another function to save the audio datafunc saveAudioData(audio:NSData, destination:NSURL) -> Bool {    if audio.writeToURL(destination, atomically: true) {        println("The file "(destination.lastPathComponent!.stringByDeletingPathExtension)" was successfully saved.")        return true    }    return false}// just convert your links to Urlsfunc linksToUrls(){    musicUrls = musicArray        .map() { NSURL(string: ) }        .filter() {  != nil }}// create a loop to start downloading your urlsfunc startDownloadingUrls(){    for url in musicUrls {        let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!)        if NSFileManager().fileExistsAtPath(destinationUrl.path!) { println("The file "(destinationUrl.lastPathComponent!.stringByDeletingPathExtension)" already exists at path.")        } else { println("Started downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".") getAudioDataFromUrl(url) { data in     dispatch_async(dispatch_get_main_queue()) {         println("Finished downloading "(url.lastPathComponent!.stringByDeletingPathExtension)".")         println("Started saving "(url.lastPathComponent!.stringByDeletingPathExtension)".")         if self.saveAudioData(data!, destination: self.documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!) ) {  // do what ever if writeToURL was successful         } else {  println("The File "(url.lastPathComponent!.stringByDeletingPathExtension)" was not saved.")         }     } }        }    }}override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.    println("Begin of pre")    linksToUrls()    startDownloadingUrls()    println("End of pre")}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5622510.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存