简体   繁体   中英

array with custom object Swift

I have class office, and want to have array of employees to it

class Office {
    var Employees: Array<Employee>
}

I also created a separated Employee.swift file, but the complier gives error of use of undeclared type "Employee" whats wrong here?

you need to declare the class Employee like this

class Employee {

  var name : String
  var age : Int

  init(name: String, age : Int)
  {
    self.name = name
    self.age = age
  }
}

class Office {
    var employees = Array<Employee>()
}

to avoid confusion variable names should always begin with a lowercase letter, class names with an uppercase letter

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