更新数组,聪明的方法吗?

我正在使用包含变量的struct 'xyz'。这些变量是array'arr_InStruct'的一部分。 “ arr_InStruct”的副本在TVC中用作dataSource

这是一个有效的程序,但我对解决方案不满意。

缺点1:我需要知道变量'indexPath'中的确切位置(arr_InStruct

缺点2:变得非常拥挤,难以理解 关于如何以更智能的方式更新array的任何建议?

数据文件

import Foundation

struct xyz {
    //-> Variablen für Array
    //didSet updates the array in struct after value of variable was changed
    static var text0:String = "Entry 0" {didSet{arr_InStruct[0][0] = text0}}//push update 'arr_InStruct' bei Änderung
    static var text1:String = "Entry 1" {didSet{arr_InStruct[0][1] = text1}}//push update 'arr_InStruct' bei Änderung
    static var text2:String = "Entry 2" {didSet{arr_InStruct[0][2] = text2}}//push update 'arr_InStruct' bei Änderung
    static var text3:String = "Entry 3" {didSet{arr_InStruct[0][3] = text3}}//push update 'arr_InStruct' bei Änderung
    //<- Variablen für Array

    //disadvantage 1: I need to know the exact position (indexPath) of the variable within 'arr_InStruct'
    //disadvantage 2: becomes very crowded und unreadable
    //is there a smarter way to archive an array update?

    //2D Array
    //es wird gefüllt mit dem Inhalt der obigen Variablen
    static var arr_InStruct:[[String]] =
    [
        [
            text0,text1,text2,text3,]
    ]//end arr_InStruct
}//end struct xyz

TVC

import UIKit

class reloadTVTest: UITableViewController {

    //MARK: - >>> Arrays w data for TV
    let arr_Header = ["Section 0"] //1D Array 

    var arr_Data  = xyz.arr_InStruct { //2D Array in 'ArrayFile.swift' -> struct 'xyz'
        didSet{ //didSet will be called every time you change something in your array
            DispatchQueue.main.async {self.tableView.reloadData()} 
        }//end didSet
    }//end var
    //MARK: <<< Arrays w data for TV


    //MARK: - >>> actions
    @IBaction func changepressed(_ sender: UIBarButtonItem) {
        print("""

            changepressed
            arr_Data in TVC:   \(arr_Data) <- Array in TVC before change
            xyz.arr_InStruct:  \(xyz.arr_InStruct) <- Array in struct before change

            """)

        xyz.text1 = "Entry changed "+randomString(length: 3) //change Variable in 'xyz'

        print("""

            xyz.text1:   '\(xyz.text1)' <- Variable changed in struct,didset called
            xyz.arr_InStruct: \(xyz.arr_InStruct) <- Array in struct changed by didSet
            arr_Data in TVC:  \(arr_Data)  <- but Array in TVC not changed
            """)

        arr_Data  = xyz.arr_InStruct //Array changed,didset called -> reloadData()

        print("""

            arr_Data in TVC was updated
            arr_Data in TVC:  \(arr_Data) <- now Array in TVC changed,didSet updated TV
            """)
    }
    //MARK: <<< actions


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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return arr_Header.count
    }


    override func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        let header = UILabel()
        header.text = arr_Header[section]
        return header
    }


    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arr_Data[section].count
    }


    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellreloadTVTest",for: indexPath)
        let name = arr_Data[indexPath.section][indexPath.row]

        // Configure the cell...
        cell.textLabel?.text = name

        return cell
    }
}//end class reloadTVTest


extension reloadTVTest { //Swift 4.2,creating random String
    func randomString(length: Int) -> String {
        let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        return String((0..<length).map{ _ in letters.randomElement()! })
    }
}//end extension reloadTVTest

结果: changing TV entries with button

hezhenhuaf1 回答:更新数组,聪明的方法吗?

首先,其中一些与您的struct XYZ一起发布,

  1. 除非您在多个地方使用同一对象,否则不需要在其中放入所有static。而是在您想使用的任何地方创建XYZ的单个实例。

  2. 不是将didSet的观察者设置为所有text0,text1,text2 and text3的人,而是将arr_InStruct设为计算属性。这样arr_InStruct将始终返回更新后的值。

所以XYZ就像

struct XYZ {
    var text0 = "Entry 0"
    var text1 = "Entry 1"
    var text2 = "Entry 2"
    var text3 = "Entry 3"

    var arr_InStruct: [[String]] {
        return [[text0,text2,text3]]
    }
}

接下来,在您的控制器reloadTVTest中创建一个XYZ的实例,并将其用作dataSource的{​​{1}}。

tableView
,

仅是示例

let newArrUsingMap = arrayToUpdate.map { $0 * 10 }

您可以使用地图功能更新整个数组,请给我有关您问题的详细信息

本文链接:https://www.f2er.com/3160239.html

大家都在问