我似乎无法创建包含用户的数组。 我必须创建和10个用户的数组.. 我该如何正确创建它?
import UIKit
enum DeviceType {
case Phone, Tablet, Watch
}
enum OperatingSystemType {
case iOS, Android, Windows
}
struct OperatingSystemVersion {
var Major: Int
var Minor: Int
var Patch: Int
}
struct OperatingSystem{
var type: OperatingSystemType
var version: OperatingSystemVersion
}
class Device {
var DeviceID: Int
var Type: DeviceType
var Operating_System: OperatingSystem
var UserID: Int
var Description: String
var InventoryNR: String
init () {
DeviceID = 1233
Type = .Phone
Operating_System = OperatingSystem(type: .iOS, version: OperatingSystemVersion(Major: 9, Minor: 0, Patch: 2))
UserID = 2
Description = "took"
InventoryNR = "no17"
}
}
class User {
var UserID: Int
var Username: String
var Location: String
var Devices: [Device]
init() {
UserID = 566
Username = "david"
Location = "Fortech"
Devices = [Device.init()]
}
}
var Users = [User] ()
Users.append(UserID: 23, Username: "David", Location: "HQ", Devices : User)
答案 0 :(得分:1)
您需要为User
课程(以及其他课程)创建有效的初始值设定项,但我们将重点关注User
此处作为示例。“
class User {
var UserID: Int
var Username: String
var Location: String
var Devices: [Device]
init(userID: Int, username: String, location: String, devices: [Device]) {
self.UserID = userID
self.Username = username
self.Location = location
self.Devices = devices
}
}
现在您可以创建一个用户:
let david = User(userID: 23, username: "David", location: "HQ", devices : [Device()])
并将其添加到您的Users
数组中:
Users.append(david)
注意:类名以大写字母开头,但变量应以小写字母开头。因此,var UserID: Int
应为var userID: Int
,Users
数组应为users
等。