0

I can't seem to create an array that contains an user. I have to create and array of 10 users.. How do I properly create it?

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)
1
  • You need to first create Users and then you can append that User. Commented Oct 7, 2015 at 19:33

1 Answer 1

1

You need to create a valid initializer for your User class (and for the other classes too but we're focusing on User here as an example).

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
    }
}

Now you can create a user:

let david = User(userID: 23, username: "David", location: "HQ", devices : [Device()])

And add it to your Users array:

Users.append(david)

Note: class names begin with uppercase letter but variables should begin with a lowercase letter. So your var UserID: Int should be var userID: Int for example, the Users array should be users, etc.

Sign up to request clarification or add additional context in comments.

5 Comments

It was working but it was always initializing an User object with the same hardcoded values. With my example you can make custom User objects.
Also method and function names should begin with a lower-case letter. The exception for variable names is that is a variable begins with a well know acronym it can be upper-case.
Thank you! I'll also change the way I declare variables and classes.
You're welcome. Just for info, there's a great resource here about good style practices for Swift: github.com/raywenderlich/swift-style-guide
Great resource indeed! :) Thanks, again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.