Function 如何将未知结构传递给函数-swift 3,IOS10
这将是我关于StackOverflow的第一个问题,也是我在Swift 3上提出的第一个问题;我是一个初学者,也许咬的比我能咀嚼的更多,但是 我正在尝试创建一个类,该类将帮助创建菜单项(该类称为“MenuItems”),以填充到IOS应用程序中的动态表中。我已经创建了这个类的大部分,它从传递给它的数据中标识了头是什么,以及每种类型中有多少个将在表中划分为多个部分。我现在正处于尝试使类更通用的阶段,因此它将适用于不同的数据结构,我可能希望在将来填充到类似的表中 我希望表中包含的数据来自其自身swift文件中的结构。看起来是这样的:Function 如何将未知结构传递给函数-swift 3,IOS10,function,data-structures,swift3,ios10,Function,Data Structures,Swift3,Ios10,这将是我关于StackOverflow的第一个问题,也是我在Swift 3上提出的第一个问题;我是一个初学者,也许咬的比我能咀嚼的更多,但是 我正在尝试创建一个类,该类将帮助创建菜单项(该类称为“MenuItems”),以填充到IOS应用程序中的动态表中。我已经创建了这个类的大部分,它从传递给它的数据中标识了头是什么,以及每种类型中有多少个将在表中划分为多个部分。我现在正处于尝试使类更通用的阶段,因此它将适用于不同的数据结构,我可能希望在将来填充到类似的表中 我希望表中包含的数据来自其自身swi
struct EquipmentStruct {
var name : String!
var serialNumber : String?
var alias : String?
var image : UIImage?
}
class MenuItems<T : SomeProtocol> {
var tableDataArray: [T] = []
func addItem(_ newItem: T) {
tableDataArray.append(newItem)
}
// ...
// since you just need guidance on creating addItems method, figure the rest yourself please! XD
}
我有一个类型为EquipmentStruct的数组,短期内,它在我的tableViewController文件中初始化(以后不再在这里),我希望在MenuItems类中创建一个公共函数,允许我根据需要向表中添加一个项
func addItem(item, dataType) // types for item and dataType are part of my question
在设计此功能时,我发现了我的问题:
var dataArray : [EquipmentStruct] =
[EquipmentStruct(name: "SA80", serialNumber:"01234-56-789", alias: "29", image: #imageLiteral(resourceName: "SA80")),
EquipmentStruct(name: "LSW", serialNumber:"11111-22-333-4444", alias: "98", image: #imageLiteral(resourceName: "LSW"))]
var tableMenuItems = MenuItems() // create instance of class MenuItems
override func viewDidLoad() {
super.viewDidLoad()
for var itemNumber in 0..<dataArray.count{
tableMenuItems.addItem(item: generalHoldingsDataArray[itemNumber], dataType: EquipmentStruct)
}
// Add new item of type 'dataType' to MenuItems.tableDataArray
// Store tableDataArrayType for use throughout the class
//
func addItem(item: [Something], dataType: SomeVariableType){
if let newItem = item as! dataType{ // cast the variable received to its type
tableDataArrayType = dataType
tableDataArray.append(newItem)
}
}
Kind认为一个表视图应该只包含一种类型的东西作为模型。单个表视图的设计目的不是显示有关
EquipmentStruct
、某个SomeOtherStruct
以及3个Foo
s的信息。你必须把所有这些归纳成一种类型
另一种可能是您希望创建一个MenuItems
类型,该类型可以创建一个显示EquipmentStruct
s的表视图,一个显示SomeOtherStruct
s的表视图,基本上是一个显示您喜欢的任何类型的表视图。如果是这种情况,您应该使用泛型
使MenuItems
类如下:
struct EquipmentStruct {
var name : String!
var serialNumber : String?
var alias : String?
var image : UIImage?
}
class MenuItems<T : SomeProtocol> {
var tableDataArray: [T] = []
func addItem(_ newItem: T) {
tableDataArray.append(newItem)
}
// ...
// since you just need guidance on creating addItems method, figure the rest yourself please! XD
}
要创建显示SomeOtherStruct
s的表视图
let tableMenuItems = MenuItems<EquipmentStruct>()
let tableMenuItems = MenuItems<SomeOtherStruct>()
这只是确保要在表视图中显示的类型具有名为
displayTitle
、displaySubtitle
、displayImage
的属性。您可以在MenuItems
类中使用这些属性来设置表视图单元格的外观。一个表视图应该只包含一种类型的内容。您是否正在尝试创建一个表视图,其中包含许多不同类型的对象作为模型?或者您正在尝试创建多个表视图,每个表视图都包含一种类型的对象作为模型,但使用相同的MenuItems
类?