I've essentially got two methods:
private func addBagToCollection(bag : VendorBag){
var array = arrayForService(bag.type)
array.append(bag)
}
And
func arrayForService(type : BAG_TYPE) -> Array<VendorBag>{
switch type{
case .bagTypeDryCleaning:
return dcBags
case .bagTypeWashAndFold:
return wfBags
case .bagTypeLaunderedShirt:
return lsBags
case .bagTypeHangDry:
return hdBags
case .bagTypeUnknown:
return unBags
}
}
The issue is that the array being referenced in addBagToCollection isn't saving the appended items, meaning that every time that I call the method, the size of the array is 0.
I initialize all my arrays at the top of the class:
var hdBags : Array<VendorBag> = [VendorBag]()
var wfBags : Array<VendorBag> = [VendorBag]()
var lsBags : Array<VendorBag> = [VendorBag]()
var dcBags : Array<VendorBag> = [VendorBag]()
var unBags : Array<VendorBag> = [VendorBag]()
but for some reason, the arrayForService function seems to either only return a copy of the array, or a newly initialized one. How do I return a reference to that array so that all changes made to it are kept even after exiting the function?
Got some more info:
So indeed it seems like arrays are copied when returned from functions--
Swift’s Array types are implemented as structures. This means that arrays
are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.
So how do I return a reference to the actual array rather than a copy of it?
Thanks!