新手:在结构中引用和初始化结构元素

Newbie: referencing and initializing structure elements within structures

好的,超级新手 Swift 学习者,我确定这是一个简单的问题,出于某种原因我无法全神贯注。

在下面的代码中,我正在创建一个(非常)简单的地址簿结构,只是为了了解如何引用和初始化各种元素,尤其是结构中的结构。我做错了什么,有没有更好的方法来写这个?我收到错误消息(使用 Swift 游乐场)。

最后三行错误:

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

此外,当我设置 var page = [AddressBookStrcut] 时出现错误:

Expected member name or constructor call after type name

提前感谢您的理解。 :)

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"

结构是值类型,请检查。 structure vs class in swift language,

你不能做这个 littleBlackBook.page[0].cellPhone,因为 cellPhone 是常量,你使用 let,而不是使用构造函数,我还更改了 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)
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)