2017-11-23 3 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 
} 

을하지만 그것을 실행할 때, 노래는 처음 뷰 컨트롤러에 표시되지 나는 두 번째보기 컨트롤러 탭 응용 프로그램 충돌을 클릭하면 상기 이유는 다음

인덱스 범위 밖에

첫 번째 뷰 컨트롤러의 코드는 다음과 같다 :

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

두 번째 뷰 컨트롤러 코드 :

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

는'songs'? 채워지는 곳은 어디입니까? 'thisSong'은 어디에서 정의 되는가? – Larme

+0

첫 번째보기 컨트롤러에서 온 것입니다. 변경되었습니다 .. 더보기 –

+0

노래보기 및 노래를 자세히보기 컨트롤러에 전달하는 방법은 무엇입니까? –

답변

0

제공된 코드에서 조건이 충분하지 않음 b 예외를 처리하고 코드의 모든 시나리오를 처리하려는 경우 예 : 값에 액세스하기 전에 항상 array.count> 0을 확인하십시오. 귀하의 경우에는 : 정의

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

+0

코드를 입력하십시오. –

+0

인사이드 viewdidload –

+0

내가 그것을 입력했지만, 내가 실행했을 때 ... 노래 목록이 나타나지 않고 자세히보기 컨트롤러에서 아무 것도 바뀌지 않습니다. –

관련 문제