0

OK, super-newbie Swift learner here with what I am sure is a simple question, that for some reason I cannot wrap my head around.

In the below code, I am creating a (very) simple address book structure just to understand how to reference and initialize the various elements, especially structures within structures. What am I doing wrong and is there a better way to write this? I am getting errors (using Swift Playground).

Errors on the last three lines:

Instance member 'page' cannot be used on the type 'AddressBookStruct'
Instance member 'subscript' cannot be used on type '[AddressPageStruct]'.

Plus, when I set var page = [AddressBookStrcut] I get the error:

Expected member name or constructor call after type name

Thanks in advance for the understanding nudge. :)

struct AddressPageStruct {
    let firstName: String
    let lastName: String
    let cellPhone: Int

    init(f: String, l:String, c:Int) {
        firstName = f
        lastName = l
        cellPhone = c
    }
}

struct AddressBookStruct {
    let color: String
    let size: String
    var page = [AddressPageStruct]
}

var littleBlackBook = AddressBookStruct.self

littleBlackBook.init(color: "Black", size: "Little")

littleBlackBook.page[0].cellPhone = 6191234567
littleBlackBook.page[0].firstName = "Bob"
littleBlackBook.page[0].lastName = "Smith"
TylerP
  • 9,600
  • 4
  • 39
  • 43
Ralph
  • 3
  • 1
  • Please share your errors. – koen May 12 '20 at 22:17
  • Errors on the last three lines: "Instance member 'page' cannot be used on the type 'AddressBookStruct'" and "Instance member 'subscript' cannot be used on type '[AddressPageStruct]'". ---- plus, when I set "var page = [AddressBookStrcut]" I get the error "Expected member name or constructor call after type name" – Ralph May 12 '20 at 22:26

2 Answers2

1

The struct is value type, please check this. structure vs class in swift language,

you can't make this littleBlackBook.page[0].cellPhone, because cellPhone is constant you use let, instead use the constructor, also I change the page for empty array of AddressPageStruct

import Cocoa

struct AddressPageStruct {
    let firstName: String
    let lastName: String
    let cellPhone: Int

    init(f: String, l:String, c:Int) {
        firstName = f
        lastName = l
        cellPhone = c
    }
}

struct AddressBookStruct {
    let color: String
    let size: String
    var page = [AddressPageStruct]()
}

var littleBlackBook = AddressBookStruct(color: "Black", size: "Little")

littleBlackBook.page.append(AddressPageStruct(f: "Bob", l: "Smith", c: 6191234567))
print(littleBlackBook.page)
cristian_064
  • 576
  • 3
  • 16
0
enter code here
    var littleBlackBook = AddressBookStruct(color: "Black", size: "Little")
    let item = AddressPageStruct(f: "Bob", l: "Smith", c:6191234567 )
    littleBlackBook.page.append(item)
    littleBlackBook.page.append(AddressPageStruct(f: "Joe", l: "Blog", c: 3123467))
    print(littleBlackBook.page[0])
    print(littleBlackBook.page[1])
    print(littleBlackBook)
DaxaR
  • 19
  • 1