如何使用数组简化此代码? (代码重用)

时间:2019-03-30 01:48:21

标签: arrays swift firebase firebase-realtime-database

我编写了一个可接收图像并播放Firebase声音的类。到目前为止,代码运行良好,但是我有一个很大的类,并且如果要包含很多内容,我需要使其更简单。

我需要帮助来创建可重用的代码或数组,在这里我可以用一个数字标记UIImageView,而不用创建多个出口和复制/粘贴代码以提供相同的功能。这是我的代码:

import UIKit
import Firebase
import AVKit
import FirebaseDatabase
import SwiftyJSON
import FirebaseRemoteConfig
class CommunityConfiguration0 {
    let Firestring_image1 = NSLocalizedString("Firestring_image1", comment: "")
    let Firestring_image2 = NSLocalizedString("Firestring_image2", comment: "")
    let Firestring_image3 = NSLocalizedString("Firestring_image3", comment: "")
    let Firestring_image4 = NSLocalizedString("Firestring_image4", comment: "")
    let Firestring_image5 = NSLocalizedString("Firestring_image5", comment: "")
    let Firestring_image6 = NSLocalizedString("Firestring_image6", comment: "")
    let Firestring_image7 = NSLocalizedString("Firestring_image7", comment: "")
    let Firestring_image8 = NSLocalizedString("Firestring_image8", comment: "")
    let Firestring_image9 = NSLocalizedString("Firestring_image9", comment: "")
    let Firestring_image10 = NSLocalizedString("Firestring_image10", comment: "")
    let Firestring_image11 = NSLocalizedString("Firestring_image11", comment: "")
    let Firestring_image12 = NSLocalizedString("Firestring_image12", comment: "")
    
    var loadingURL: URL?
    var loadingURL2: URL?
    var loadingURL3: URL?
    var loadingURL4: URL?
    
    var image1URL: URL?
    var image2URL: URL?
    var image3URL: URL?
    var image4URL: URL?
    var image5URL: URL?
    var image6URL: URL?
    var image7URL: URL?
    var image8URL: URL?
    var image9URL: URL?
    var image10URL: URL?
    var image11URL: URL?
    var image12URL: URL?
    
    var sound1URL: URL?
    var sound2URL: URL?
    var sound3URL: URL?
    var sound4URL: URL?
    var sound5URL: URL?
    var sound6URL: URL?
    var sound7URL: URL?
    var sound8URL: URL?
    var sound9URL: URL?
    var sound10URL: URL?
    var sound11URL: URL?
    var sound12URL: URL?

    init(snapshot: DataSnapshot) {
        if let dict = snapshot.value as? [String: Any] {
            let json = JSON(dict)
            image1URL = URL(string: json[Firestring_image1].stringValue)
            image2URL = URL(string: json[Firestring_image2].stringValue)
            image3URL = URL(string: json[Firestring_image3].stringValue)
            image4URL = URL(string: json[Firestring_image4].stringValue)
            image5URL = URL(string: json[Firestring_image5].stringValue)
            image6URL = URL(string: json[Firestring_image6].stringValue)
            image7URL = URL(string: json[Firestring_image7].stringValue)
            image8URL = URL(string: json[Firestring_image8].stringValue)
            image9URL = URL(string: json[Firestring_image9].stringValue)
            image10URL = URL(string: json[Firestring_image10].stringValue)
            image11URL = URL(string: json[Firestring_image11].stringValue)
            image12URL = URL(string: json[Firestring_image12].stringValue)
        }
    }
}
class FirebaseViewController0: UIViewController, AVAudioPlayerDelegate, GKGameCenterControllerDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate {
    var player: AVPlayer!
    var configuration: CommunityConfiguration?

    var firesound1 = AVPlayer()
    var firesound2 = AVPlayer()
    var firesound3 = AVPlayer()
    var firesound4 = AVPlayer()
    var firesound5 = AVPlayer()
    var firesound6 = AVPlayer()
    var firesound7 = AVPlayer()
    var firesound8 = AVPlayer()
    var firesound9 = AVPlayer()
    var firesound10 = AVPlayer()
    var firesound11 = AVPlayer()
    var firesound12 = AVPlayer()
    
    @IBOutlet weak var firelabel1: UIButton?
    @IBOutlet weak var firelabel2: UIButton?
    @IBOutlet weak var firelabel3: UIButton?
    @IBOutlet weak var firelabel4: UIButton?
    @IBOutlet weak var firelabel5: UIButton?
    @IBOutlet weak var firelabel6: UIButton?
    @IBOutlet weak var firelabel7: UIButton?
    @IBOutlet weak var firelabel8: UIButton?
    @IBOutlet weak var firelabel9: UIButton?
    @IBOutlet weak var firelabel10: UIButton?
    @IBOutlet weak var firelabel11: UIButton?
    @IBOutlet weak var firelabel12: UIButton?
    
    @IBOutlet weak var fireimage1: UIImageView!
    @IBOutlet weak var fireimage2: UIImageView!
    @IBOutlet weak var fireimage3: UIImageView!
    @IBOutlet weak var fireimage4: UIImageView!
    @IBOutlet weak var fireimage5: UIImageView!
    @IBOutlet weak var fireimage6: UIImageView!
    @IBOutlet weak var fireimage7: UIImageView!
    @IBOutlet weak var fireimage8: UIImageView!
    @IBOutlet weak var fireimage9: UIImageView!
    @IBOutlet weak var fireimage10: UIImageView!
    @IBOutlet weak var fireimage11: UIImageView!
    @IBOutlet weak var fireimage12: UIImageView!
    
    var configurationHandle: DatabaseHandle?
    var configurationReference: DatabaseReference?
//    let PlayAgainObserver = NotificationCenter.default
//    let notification = NotificationCenter.default
//    let ncObserver = NotificationCenter.default
//    let FirebaseObserver = NotificationCenter.default

    let Firestring_text1 = NSLocalizedString("Firestring_text1", comment: "")
    let Firestring_text2 = NSLocalizedString("Firestring_text2", comment: "")
    let Firestring_text3 = NSLocalizedString("Firestring_text3", comment: "")
    let Firestring_text4 = NSLocalizedString("Firestring_text4", comment: "")
    let Firestring_text5 = NSLocalizedString("Firestring_text5", comment: "")
    let Firestring_text6 = NSLocalizedString("Firestring_text6", comment: "")
    let Firestring_text7 = NSLocalizedString("Firestring_text7", comment: "")
    let Firestring_text8 = NSLocalizedString("Firestring_text8", comment: "")
    let Firestring_text9 = NSLocalizedString("Firestring_text9", comment: "")
    let Firestring_text10 = NSLocalizedString("Firestring_text10", comment: "")
    let Firestring_text11 = NSLocalizedString("Firestring_text11", comment: "")
    let Firestring_text12 = NSLocalizedString("Firestring_text12", comment: "")
    
    func updateViewWithRCValues() {
        let buttonLabel1 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text1).stringValue ?? ""
        firelabel1?.setTitle(buttonLabel1, for: .normal)
        let buttonLabel2 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text2).stringValue ?? ""
        firelabel2?.setTitle(buttonLabel2, for: .normal)
        let buttonLabel3 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text3).stringValue ?? ""
        firelabel3?.setTitle(buttonLabel3, for: .normal)
        let buttonLabel4 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text4).stringValue ?? ""
        firelabel4?.setTitle(buttonLabel4, for: .normal)
        let buttonLabel5 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text5).stringValue ?? ""
        firelabel5?.setTitle(buttonLabel5, for: .normal)
        let buttonLabel6 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text6).stringValue ?? ""
        firelabel6?.setTitle(buttonLabel6, for: .normal)
        let buttonLabel7 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text7).stringValue ?? ""
        firelabel7?.setTitle(buttonLabel7, for: .normal)
        let buttonLabel8 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text8).stringValue ?? ""
        firelabel8?.setTitle(buttonLabel8, for: .normal)
        let buttonLabel9 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text9).stringValue ?? ""
        firelabel9?.setTitle(buttonLabel9, for: .normal)
        let buttonLabel10 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text10).stringValue ?? ""
        firelabel10?.setTitle(buttonLabel10, for: .normal)
        let buttonLabel11 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text11).stringValue ?? ""
        firelabel11?.setTitle(buttonLabel11, for: .normal)
        let buttonLabel12 = RemoteConfig.remoteConfig().configValue(forKey: Firestring_text12).stringValue ?? ""
        firelabel12?.setTitle(buttonLabel12, for: .normal)
    }
    
    func setupRemoteConfigDefaults() {
        let defaultValues = [
            "label1": "" as NSObject,
            "label2": "" as NSObject,
            "label3": "" as NSObject,
            "label4": "" as NSObject,
            "label5": "" as NSObject,
            "label6": "" as NSObject,
            "label7": "" as NSObject,
            "label8": "" as NSObject,
            "label9": "" as NSObject,
            "label10": "" as NSObject,
            "label11": "" as NSObject,
            "label12": "" as NSObject
        ]
        RemoteConfig.remoteConfig().setDefaults(defaultValues)
    }
    func fetchRemoteConfig() {
        // Remove this before production!!
        let debugSettings = RemoteConfigSettings(developerModeEnabled: true)
        RemoteConfig.remoteConfig().configSettings = debugSettings
        RemoteConfig.remoteConfig().fetch(withExpirationDuration: 0) { [unowned self] (status, error) in guard error == nil else {
            print ("Error fetching remote values: \(String(describing: error))")
            return
            }
            print("Retrieved values from the cloud")
            RemoteConfig.remoteConfig().activateFetched()
            self.updateViewWithRCValues()
        }
    }
    @objc func stopObserving() {
        if let configurationHandle = configurationHandle {
            configurationReference?.removeObserver(withHandle: configurationHandle)
        }
    }
    @objc func startObserving() {
        stopObserving()
      configurationReference = Database.database(url: "https://mlg-soundboard-2018-edition.firebaseio.com").reference().child("screens").child("us").child("community")
        configurationHandle = configurationReference?.observe(.value, with: { [weak self] (snapshot) in
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image1URL {
                    self?.fireimage1?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image2URL {
                    self?.fireimage2?.hnk_setImage(from: url)
                }
            }
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image3URL {
                    self?.fireimage3?.hnk_setImage(from: url)
                }
            }
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image4URL {
                    self?.fireimage4?.hnk_setImage(from: url)
                }
            }
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image5URL {
                    self?.fireimage5?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image6URL {
                    self?.fireimage6?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image7URL {
                    self?.fireimage7?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image8URL {
                    self?.fireimage8?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image9URL {
                    self?.fireimage9?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image10URL {
                    self?.fireimage10?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image11URL {
                    self?.fireimage11?.hnk_setImage(from: url)
                }}
            DispatchQueue.main.async {
                self?.configuration = CommunityConfiguration(snapshot: snapshot)
                if let url = self?.configuration?.image12URL {
                    self?.fireimage12?.hnk_setImage(from: url)
                }}
        })
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupRemoteConfigDefaults()
        fetchRemoteConfig()
        startObserving()
    }
    deinit {
        stopObserving()
    }

    @IBAction func soundpressed1(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound1URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound1 = AVPlayer(playerItem: playerItem)
            firesound1.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound1.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound1URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound1 = AVPlayer(playerItem: playerItem)
            firesound1.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound1.play()
        }
}
    @IBAction func soundpressed2(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound2URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound2 = AVPlayer(playerItem: playerItem)
            firesound2.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound2.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound2URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound2 = AVPlayer(playerItem: playerItem)
            firesound2.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound2.play()
        }
    }
    @IBAction func soundpressed3(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound3URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound3 = AVPlayer(playerItem: playerItem)
            firesound3.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound3.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound3URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound3 = AVPlayer(playerItem: playerItem)
            firesound3.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound3.play()
        }
    }
    @IBAction func soundpressed4(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound4URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound4 = AVPlayer(playerItem: playerItem)
            firesound4.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound4.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound4URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound4 = AVPlayer(playerItem: playerItem)
            firesound4.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound4.play()
        }
    }
    @IBAction func soundpressed5(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound5URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound5 = AVPlayer(playerItem: playerItem)
            firesound5.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound5.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound5URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound5 = AVPlayer(playerItem: playerItem)
            firesound5.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound5.play()
        }
    }
    @IBAction func soundpressed6(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound6URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound6 = AVPlayer(playerItem: playerItem)
            firesound6.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound6.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound6URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound6 = AVPlayer(playerItem: playerItem)
            firesound6.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound6.play()
        }
    }
    @IBAction func soundpressed7(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound7URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound7 = AVPlayer(playerItem: playerItem)
            firesound7.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound7.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound7URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound7 = AVPlayer(playerItem: playerItem)
            firesound7.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound7.play()
        }
    }
    @IBAction func soundpressed8(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound8URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound8 = AVPlayer(playerItem: playerItem)
            firesound8.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound8.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound8URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound8 = AVPlayer(playerItem: playerItem)
            firesound8.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound8.play()
        }
    }
    @IBAction func soundpressed9(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound9URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound9 = AVPlayer(playerItem: playerItem)
            firesound9.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound9.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound9URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound9 = AVPlayer(playerItem: playerItem)
            firesound9.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound9.play()
        }
    }
    @IBAction func soundpressed10(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound10URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound10 = AVPlayer(playerItem: playerItem)
            firesound10.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound10.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound10URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound10 = AVPlayer(playerItem: playerItem)
            firesound10.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound10.play()
        }
    }
    @IBAction func soundpressed11(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound11URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound11 = AVPlayer(playerItem: playerItem)
            firesound11.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound11.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound11URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound11 = AVPlayer(playerItem: playerItem)
            firesound11.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound11.play()
        }
    }
    @IBAction func soundpressed12(_ sender: Any) {
        if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == true {
            guard let url = configuration?.sound12URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound12 = AVPlayer(playerItem: playerItem)
            firesound12.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound12.play()
        }
        else if UserDefaults.standard.bool(forKey: "SoundKeyOnline") == false {
            stopSounds()
            guard let url = configuration?.sound12URL else { return }
            let playerItem = CachingPlayerItem(url: url)
            firesound12 = AVPlayer(playerItem: playerItem)
            firesound12.automaticallyWaitsToMinimizeStalling = false
            playerItem.download()
            firesound12.play()
        }
    }

    @objc func stopSounds() {
        firesound1.replaceCurrentItem(with: nil)
        firesound2.replaceCurrentItem(with: nil)
        firesound3.replaceCurrentItem(with: nil)
        firesound4.replaceCurrentItem(with: nil)
        firesound5.replaceCurrentItem(with: nil)
        firesound6.replaceCurrentItem(with: nil)
        firesound7.replaceCurrentItem(with: nil)
        firesound8.replaceCurrentItem(with: nil)
        firesound9.replaceCurrentItem(with: nil)
        firesound10.replaceCurrentItem(with: nil)
        firesound11.replaceCurrentItem(with: nil)
        firesound12.replaceCurrentItem(with: nil)
    }

    override func didReceiveMemoryWarning() {
    }
}

如您所见,剪切和粘贴操作相当繁琐,但是实际逻辑却很少。我该如何简化?

1 个答案:

答案 0 :(得分:0)

您可以将“参考插座集合”替换为“参考插座”。 然后

@IBOutlet weak var firelabel1: UIButton?
@IBOutlet weak var firelabel2: UIButton?
@IBOutlet weak var firelabel3: UIButton?
@IBOutlet weak var firelabel4: UIButton?
@IBOutlet weak var firelabel5: UIButton?
@IBOutlet weak var firelabel6: UIButton?
@IBOutlet weak var firelabel7: UIButton?
@IBOutlet weak var firelabel8: UIButton?
@IBOutlet weak var firelabel9: UIButton?
@IBOutlet weak var firelabel10: UIButton?
@IBOutlet weak var firelabel11: UIButton?
@IBOutlet weak var firelabel12: UIButton?

@IBOutlet weak var fireimage1: UIImageView!
@IBOutlet weak var fireimage2: UIImageView!
@IBOutlet weak var fireimage3: UIImageView!
@IBOutlet weak var fireimage4: UIImageView!
@IBOutlet weak var fireimage5: UIImageView!
@IBOutlet weak var fireimage6: UIImageView!
@IBOutlet weak var fireimage7: UIImageView!
@IBOutlet weak var fireimage8: UIImageView!
@IBOutlet weak var fireimage9: UIImageView!
@IBOutlet weak var fireimage10: UIImageView!
@IBOutlet weak var fireimage11: UIImageView!
@IBOutlet weak var fireimage12: UIImageView!

将变为

@IBOutlet var firelabels: [UIButton]!
@IBOutlet var fireimages: [UIImageView]!

然后 使用工厂方法创建“ CommunityConfiguration”和&“ playerItem”

例如

lazy var fireString_image_arr: [String] = {
    var image_arr = [String]()
    for i in 1...12 {
        image_arr.append(NSLocalizedString("Firestring_image\(i)", comment: ""))
    }
    return image_arr
}()

var loadingURL = [URL]()
var imageURL_arr = [URL]()
var soundURL = [URL]()

init(snapshot: DataSnapshot) {
    if let dict = snapshot.value as? [String: Any] {
        let json = JSON(dict)
        for i in 0...11 {
            if let url = URL.init(string: json[fireString_image_arr[i]].stringValue) {
                imageURL_arr.append(url)
            }
        }
    }
}



func updateViewWithRCValues() {
        for i in 0...11 {
            let tmp_string = NSLocalizedString("Firestring_text\(i + 1)", comment: "")
            let button_label = RemoteConfig.remoteConfig().configValue(forKey: tmp_string).stringValue ?? ""
            firelabels[i].setTitle(button_label, for: .normal)
        }
    }

所有按钮都可以使用相同的操作

var fireSound_arr = [AVPlayer]()

@IBAction func soundpressed(_ sender: Any) {
        if let configuration = self.configuration,
            let index = firelabels.firstIndex(of: sender as! UIButton) {

        }
}

类似的东西 您需要继续改进