2017-11-23 20 views
0

xcodeに追加したフォルダからいくつかの曲を再生したいと思います。swift:インデックスが範囲外です

func playThis(thisOne:String) 
{ 
    do 
    { 
     let audioPath = Bundle.main.path(forResource: thisOne, ofType: ".mp3") 
     try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
     audioPlayer.play() 
    } 
    catch 
    { 
     print ("ERROR") 
    } 
} 

override func viewDidLoad() { 

    super.viewDidLoad() 
    label.text = songs[thisSong] //error index out of range 
} 

しかし、私はそれを実行すると、曲が最初 ビューコントローラに表示されたときに、私は2番目のビューコントローラ]タブアプリのクラッシュをクリックしない:私はタブ付きのアプリケーションおよびコードは次のようである使用

その理由は次のとおりです。

範囲外のインデックス

ファーストビューコントローラのコードは次のようである:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    do 
    { 
     let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3") 
     try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
     audioPlayer.play() 
     thisSong = indexPath.row 
     audioStuffed = true 
    } 
    catch 
    { 
     print ("ERROR") 
    } 
} 

第2のビューコントローラコード:

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    gettingSongNames() 
} 


override func didReceiveMemoryWarning() 
{ 
    super.didReceiveMemoryWarning() 
} 


//FUNCTION THAT GETS THE NAME OF THE SONGS 
func gettingSongNames() 
{ 
    let folderURL = URL(fileURLWithPath:Bundle.main.resourcePath!) 

    do 
    { 
     let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) 

     //loop through the found urls 
     for song in songPath 
     { 
      var mySong = song.absoluteString 

      if mySong.contains(".mp3") 
      { 
       let findString = mySong.components(separatedBy: "/") 
       mySong = findString[findString.count-1] 
       mySong = mySong.replacingOccurrences(of: "%20", with: " ") 
       mySong = mySong.replacingOccurrences(of: ".mp3", with: "") 
       songs.append(mySong) 
      } 

     } 

     myTableView.reloadData() 
    } 
    catch 
    { 
     print ("ERROR") 
    } 
} 

import UIKit 
import AVFoundation 

var audioPlayer = AVAudioPlayer() 
var songs:[String] = [] 
var thisSong = 0 
var audioStuffed = false 

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var myTableView: UITableView! 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return songs.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell") 
    cell.textLabel?.text = songs[indexPath.row] 
    return cell 
} 

と私の方法は、詳細ビューコントローラに曲を渡します

+0

?それは人口はどこにありますか? 'thisSong'の定義はどこですか? – Larme

+0

これは最初のビューコントローラーからのもので、私は変更されています。もっと見る –

+0

曲のインデックスと曲をディテールのView Controllerにどのように渡しますか? –

答えて

0

提供されたコードから十分な条件が得られないb例外を処理し、コードのすべてのシナリオを処理するクラッシュを停止したい場合 例:値にアクセスする前に必ずarray.count> 0をチェックしてください。あなたのケースでは : `songs`定義されて

if songs.count > 0 { label.text = songs[thisSong] }

+0

を参照してください。 –

+0

inside viewdidload –

+0

私はそれを入力しましたが、実行すると...曲のリストは表示されず、詳細表示のコントローラーでは何も変わらないので何も変わりません... –

関連する問題