Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用字符串值作为对象名?_Swift - Fatal编程技术网

Swift 如何使用字符串值作为对象名?

Swift 如何使用字符串值作为对象名?,swift,Swift,我有很多对象,我需要使用其中的一个,这取决于用户按什么填充我的表视图。为了减少if语句的使用,我认为将所需对象的名称存储在字符串变量上可能会有所帮助。当需要填充表视图时,将使用字符串变量,而不是检查哪个Approvate对象名 // objects defined here from different classes. var currentSection : String @IBAction func button(_ sender: UIButton) { i

我有很多对象,我需要使用其中的一个,这取决于用户按什么填充我的表视图。为了减少if语句的使用,我认为将所需对象的名称存储在字符串变量上可能会有所帮助。当需要填充表视图时,将使用字符串变量,而不是检查哪个Approvate对象名

// objects defined here from different classes.

    var currentSection : String 

@IBAction func button(_ sender: UIButton) {
        if sender.tag == 1 { //
            currentSection = "object1"
        }
        else if sender.tag == 2 { //
            currentSection = "object2"
        }
        .........etc 
    }

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {

       return(currentSection.item.count) 
}
这就是我所做的,但它不起作用,错误:“String”类型的值没有成员“item”

有人告诉我如何告诉编译器使用字符串“value”作为对象名吗?

因为currentSection是一个字符串,它对对象一无所知,所以currentSection.item没有任何意义。相反,您可以使用一个将字符串键与表示节中数据的值相关联的字符串

struct RowItem {} // the thing that represents the text for your table view row

class MyViewController {
  var sectionData: [String: [RowItem]] = [:]  // a dictionary that associates string keys to arrays of RowItem
  var currentSection: String = ""

  func tableView(_ tableView, numberOfRowsInSection section: Int) -> Int {
    let thisSection = sectionData[currentSection] ?? [] // you might not have data in the dictionary for currentSection
    return thisSection.count
  }
}
使现代化 用于支持表视图单元格的类型是什么?在cellForRow中,您必须使用一些文本、图像等来配置单元格。-无论您使用的对象的类型是什么,我在上面使用的RowItem都应该是字典中的类型-[String:[MyRowTypeWhateverThatIs]]。因此,每个字符串键映射到一个值,该值是一个数据数组。此数组中的项将对应于表视图中的单元格

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let dataForKey: [RowItemOrWhatever] = sectionData[currentSection] ?? []  // this array contains items that correspond 1:1 to the cells in your table view
    if indexPath.row >= dataForKey.count { return UITableViewCell() } // it's possible that this index could be outside the bounds of dataForKey, so we need to handle that case sensibly

    let cell = tableView.dequeueReusableCell(withIdentifier: reusableCellIdentifier, for: indexPath)
    let item = dataForKey[indexPath.row]
    cell.textLabel?.text = item.text    // or whatever it is that you're doing with these pieces of data
    return cell
  }
这样,您就可以使用numberOfRows中数组的计数并将其索引到数组中,以获取cellForRow中特定行的值。就IndexPath而言,您只想使用它在cellForRow中索引到您自己的数据——您不需要存储索引路径。您将希望以这样的方式定义RowItem,即它将具有配置单元格所需的数据。因此,如果我的单元格有一些主文本、一些细节文本和一个图像,我会像这样定义RowItem:

struct RowItem {
  var mainText: String
  var detailText: String
  var image: UIImage
}

我会使用一个枚举来返回对象作为名称。简短回答:你不能这样做。将currentSection的声明从String更改为object1和object2是什么。然后为对象指定一个引用,而不是变量名。=这非常粗略。要么如此,要么将currentSection设置为可选。在这种情况下,我看不出它有什么不同。@jefflovejapan很抱歉,我没有想到。我是一个初学者,我对字典的了解很简单。你能在你的代码中解释关于struct RowItem{}的更多信息吗?我试图定义一个[String:AnyObject]的字典,但是我得到了一个错误,AnyObject'不是这一行的'NSIndexPath'的子类型:让p=currentObject.item[indexPath.row]