1

Why does this always create an array with one object in it and can it return an actual empty object array or am I doing something wrong?

public class Client{
    var Company=""
    var FirstName=""
    var LastName=""
    var ClientID=""

    func Client()
    {
    }
}

var  clientList = [Client()]

print( clientList.count ) // 1
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Are you sure it makes sense for a Client to be named `"", ""`, with id `""` and company `""`, as a default? – Alexander Feb 07 '18 at 22:08
  • If this was truly a duplicate question it would not have been viewed 39 times as of this comment. Clearly other people had the same question. – Todd Cormack Feb 08 '18 at 18:46
  • That's what duplicate questions are for. They're signposts that lead people to the first instance of that question. And to be fair, 39 views is little, when compared to the 94,878 views of the first instance of this question – Alexander Feb 08 '18 at 19:32
  • @ToddCormack How does view count have anything to do with if it's a duplicate question or not? When you post a new question, it automatically goes to the top of the index page for the tags you listed. I mean... I would understand if it had 39 _upvotes_, but views don't mean much. Also, how is linking to a duplicate question in any way _not_ helpful? – GrumpyCrouton Feb 08 '18 at 21:06
  • @ToddCormack It seems to me that you think getting closed as a duplicate is a bad thing. It's not. There are many questions that have 10s, maybe 100s of duplicates, which are kept around and made to link back to the original. Doing that makes it easier for people to find answers, without us having to repeat the same answers on every duplicate. It also means that the centralized answers on the original question are usually higher quality, and updated more often. – Alexander Feb 08 '18 at 21:27

1 Answers1

1

Change

var clientList = [Client()]

to

var clientList: [Client] = []

or, alternatively:

var clientList = [Client]()

Client() in [Client()] is creating a new object of Client type, and using it in array literal creates an array with a single object.

If you move oval brackets outside, as in [Client](), the semantics change - now the [Client] a type of Array<Client> upon which you call an initializer.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90