在 segue 之后更新类属性中的数组



我有一个UIViewController,其中包含一个UITableView,该视图通过自定义类对象数组填充。这些自定义类对象具有数组属性。从下面的代码中可以看出,对象是相等的。当我转到第二个 vC 时,自定义类对象的数组(显然还有与每个对象关联的数组属性(将被传递。

我在第二个 vC 中有一个函数,它将对象与数组中包含的对象匹配。匹配后,将更新匹配的数组中对象的属性数组。但是,当我打印应该是更新的属性数组时,没有进行任何更改。下面是我的代码的表示形式:

class Object: Equatable {
var propertyArray: [String] = []
static func ==(lhs: object, rhs: object) -> Bool {
return lhs.someProperty == rhs.someProperty 
}
}
class ArrayOfObjects {
var array: [Object] = []
}
class vC1: UIViewController, UITableViewDelegate, UITableViewDataSource {
var objectArray1 = ArrayOfObjects()
override viewDidLoad() {
//populate objectArray1.array
}
prepareForSegue(segue: UIStoryboardSegue) {
segue.destination.objectArray2 = objectArray1 //segue to vC2
}
}
class vC2: UIViewController {
var objectArray2 = ArrayOfObjects()
var someOtherObject = Object() //object that has same properties as an object in objectArray2
func someFunc() {
let indexOfMatchingObject = objectArray2.array.index(of: someOtherObject)
let matchingObject = objectArray2.array[indexOfSomeOtherObject]
matchingObject.propertyArray.append("TestString")
print("(matchingObejct.propertyArray)") //prints []
}
}

为什么它不打印["测试字符串"]?当我从数组中删除其中一个值时也是如此,更新不会发生。

如果您想知道我为什么要这样做,那是因为对象在第二个 vC 中被修改,并且第一个 vC 中 tableView 单元格的 UI 取决于对象的属性。因此,为什么我的数据表示为类(引用类型(。

更新您的someFunc()

func someFunc() {
//try to find the same object
let indexOfMatchingObject = objectArray2.array.index(of: someOtherObject)
//obtain this common objec from array
let matchingObject = objectArray2.array[indexOfSomeOtherObject]
//update propertyArray from matchingObject
matchingObject.propertyArray.append("TestString")    
}

同时在viewWillApear中更新您的第一个 VC 表视图。

我意识到我的问题的答案与我创建的类或 segue 无关,但实际上与数组的状态有关。不能追加到尚未初始化的数组。为了附加到它,您必须首先初始化它,然后您可以附加到它。下面提供了游乐场代码,以在上述问题的上下文中演示这一点:

//Create object class
class Object: Equatable {
var propertyArray: [String]?
var id: Int = Int()
static func ==(lhs: Object, rhs: Object) -> Bool {
return lhs.id == rhs.id
}
}
//Create class that will hold array of objects
class ArrayOfObjects {
var array: [Object] = []
}

//initialise class that will hold array of objects
var array = ArrayOfObjects()
//set the initial values for the array
func setArray() {
let object1 = Object()
object1.id = 1
object1.propertyArray = ["hello"]
let object2a = Object()
object2a.id = 2
object2a.propertyArray = ["bye"]
array.array = [object1, object2a]
}
setArray()

//Create new object that will be used to match with one already in the array
let object2b = Object()
object2b.id = 2
object2b.propertyArray = ["bye"]
//Find if the new object exists in the array
let index = array.array.index(of: object2b)
let matchingObject = array.array[index!]
matchingObject.propertyArray?.append("welcome")
//We were able to append to the matchingObject (object2a) because the property array had been initialised
print(matchingObject.propertyArray) //prints ["bye", "welcome"]
//Create new object with uninitialised propertyArray
let object3a = Object()
object3a.id = 4
//Append this new object to the array
array.array.append(object3a)
//Create another new object that will be used to match with object3a
var object3b = Object()
object3b.id = 4
//Find if object3b can be matched in the array
let index2 = array.array.index(of: object3b)
let matchingObject2 = array.array[index2!]
matchingObject2.propertyArray?.append("hello")
//A match was found for object3b, but the array had not been initialised and so we couldn't append to it
print(matchingObject2.propertyArray) //prints nil
//Initialise the array
matchingObject2.propertyArray = []
matchingObject2.propertyArray?.append("goodbye")
//We can now append to it as it has been initialised
print(matchingObject2.propertyArray) //prints ["goodbye"]

最新更新