简体   繁体   中英

Error for [TableItem] is not convertible to () error in swift with array

I have an array where I'm trying to flatten a set of menu_headers that looks like this (and based upon the answer here: https://stackoverflow.com/a/30761903/152825 ):

  func processAsList(){
    println("about to process as list")
    var tmpItems:[TableItem] = [EKMenuHeader(), EKMenuItem()]

    for menuHeader in self.menuHeaders {
      println("name #1: \(menuHeader.name)")
      tmpItems.append(menuHeader)
      for menuHeader1 in menuHeader.menuHeaders {
        println("name #2: \(menuHeader1.name)")
        tmpItems.append(menuHeader1)
      }
    }
    return tmpItems
  }

but get an error that [TableItem] is not convertible to '()' . What am I doing wrong?

It's really just a matter of speaking elementary Swift. You have declared a function:

func processAsList(){

That declaration means, among other things: "This function returns no value". Therefore, you may not say the words return tmpItems , as you are trying to do.

If your function returns something, you must declare that fact, up front:

func processAsList() -> SomeKindOfValue {

It looks to me like in your case that would be

func processAsList() -> [TableItem] {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM