Исключить файлы в Xcode - Swift 4 - PullRequest
0 голосов
/ 24 апреля 2018

Привет, ребята. Мне нужно исключить некоторые mp3-файлы из проекта, я попытался с помощью mySong = mySong.replacingOccurrences(of: "Mr. Blue Sky", with: ""), но в моем табличном представлении всегда есть пустое кликабельное пространство, которое воспроизводит .mp3.Под Вы можете найти полный код.Этот код основан на получении информации через группы и URL.Этот файл должен быть исключен только в этом Swift.Пожалуйста, помогите мне.

import UIKit
import AVFoundation

class viewHappy: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var myTableView2: 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
    }

    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 happyPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

            //loop through the found urls
            for happySong in happyPath
            {
                var mySong = happySong.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)
                    print (findString)
                }

            }

            myTableView2.reloadData()
        }
        catch
        {
            print ("ERROR")
        }
        var session = AVAudioSession.sharedInstance()
        do
        {
            try session.setCategory(AVAudioSessionCategoryPlayback)

        }
        catch
        {

        }
    }
}

1 Ответ

0 голосов
/ 24 апреля 2018

Если у вас есть список песен, скажем…

struct Song {
    let title: String
    let mp3: MP3
}

let songs = [Song(title: "Mr. Blue Sky", mp3: mp3_1),
             Song(title: "Evil Woman", mp3: mp3_2),
             Song(title: "10538 Overture", mp3: mp3_3)]

, вы можете удалить песню из своего списка, используя filter

let filteredSongs = songs.filter { $0.title != "Mr. Blue Sky" }

Затем в виде таблицыделегат, верните filteredSongs.count как ваш numberOfRows

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...