Home > Blockchain >  How to return struct in swift?
How to return struct in swift?

Time:03-26

This is my struct:

struct State {
  var current: StateEntry?
  var rStack: [StateEntry] = []
  var uStack: [StateEntry] = []
}

and i have a func that has return type State:

func createEmptyState() -> State {
  var state = State.self
  return  state.current == nil, state.rStack == [], state.uStack == []//How do I return these values of struct in this function?
}

CodePudding user response:

To return null object

return State()

To return object containing something.

return State(current: StateEntry(), rStack: [], uStack: [])

CodePudding user response:

return State(current: nil, rStack: [], uStack: [])

or

return State.init()

CodePudding user response:

As all properties have default values you can simply return State()

func createEmptyState() -> State {
    return State()
}
  • State.self is wrong. It represents the type.
  • The return argument is wrong, too. The syntax doesn't exist.
  • Related