我在编写一个看似简单的任务时遇到了困难。我想将新的客户端配置文件数据添加到客户端配置文件字典(clientDatabase)中,但仍会出现错误-似乎无法追加-错误:类型为“(String:client profile)”的值没有成员“append”(请参阅代码底部的错误)
我们非常感谢您提供的任何见解。
谢谢,
比尔

//: Playground - noun: a place where people can play

import UIKit
import Foundation



/*
code copied from B-C Dev Database - structs but simplified with fewer variables
goal is to getappend new client to work.
*/

/*
Globals: these go in main.swift file
*/

struct clientProfile {

    var firstName: String = ""
    var lastName: String = ""
    var group: Int = 0
}
//var clientDatabase : NSMutableDictionary! = [String:clientProfile]()
var clientDatabase:[String:clientProfile]

/* sample data template: phone is key, sub array is;
(firstName: "", lastName: "",pilatesGroup:  )
*/
clientDatabase = [
    "1234567": clientProfile(firstName: "Sally", lastName: "Sillious", group: 3),
    "2345678": clientProfile(firstName: "Sue", lastName: "Parker",group: 8),
    "3456789": clientProfile(firstName: "Bob", lastName: "Parker",  group: 2),
    "5678901": clientProfile(firstName: "Jim", lastName: "Beam", group: 12)
        ]

clientDatabase.count

// so far so good

/*
add new client
declare local variables in scene swift files where used
*/
var firstName: String = ""
var phone:String = ""
var newPhone: String = ""
var newFirstName: String = ""
var newLastName: String = ""
var newGroup: Int = 0
// define struct using these input variables for values but with same keys as (global) clientDatabase

struct newClientProfile {

    var firstName: String = newFirstName
    var lastName: String = newLastName
    var group: Int = newGroup
}

// put newClientProfile into newClientDictionary
var newClientDatabase:Dictionary = [String:newClientProfile]()

// input values from scene - UITextFields
newPhone = "4567890"
newFirstName = "Super"
newLastName = "Dave"
newGroup = 14

// test that all values are where they should be
clientDatabase
clientDatabase.count
newClientDatabase = [newPhone:newClientProfile()]
newClientDatabase.count
// ok so far

//the following line returns an error
clientDatabase.append(newClientDatabase)
// can't seem to append - error value of type '(String: clientProfile)' has no member 'append'

最佳答案

两件事。首先clientDatabase是一个没有append的字典,相反,您必须遍历另一个字典并将其元素插入到clientDatabase中。
另一个问题是clientDatabasenewClientDatabase不是同一类型。第一个是[String : clientProfile],第二个是[String : newClientProfile]。必须将值从一种类型转换为另一种类型,才能组合词典。
对代码进行更深入的研究发现了一些关于语言的误解。例如:

struct newClientProfile {

  var firstName: String = newFirstName
  var lastName: String = newLastName
  var group: Int = newGroup
}

// put newClientProfile into newClientDictionary
var newClientDatabase:Dictionary = [String:newClientProfile]()

当您已经拥有clientProfile时,您创建的结构只是为了包含一组值。相反,你可以:
var newClientProfile = clientProfile(firstName: newFirstName, lastName: newLastName, group: newGroup)

这将创建一个变量,它是clientProfile的一个实例,并存储所需的信息。但是,其他变量定义为空值。
这是您的代码的一个清理版本,请看一看,如果您有任何问题,请告诉我。
struct ClientProfile { // Convention is to use initial caps for enum, struct, class
  let firstName: String
  let lastName: String
  let group: Int
}

var clientDatabase = [
  "1234567": ClientProfile(firstName: "Sally", lastName: "Sillious", group: 3),
  "2345678": ClientProfile(firstName: "Sue", lastName: "Parker",group: 8),
  "3456789": ClientProfile(firstName: "Bob", lastName: "Parker",  group: 2),
  "5678901": ClientProfile(firstName: "Jim", lastName: "Beam", group: 12)
]

// input values from scene - UITextFields
let newPhone = "4567890"
let newFirstName = "Super"
let newLastName = "Dave"
let newGroup = 14

// define struct using these input variables for values but with same keys as (global) clientDatabase
let newClientProfile = ClientProfile(firstName: newFirstName, lastName: newLastName, group: newGroup)
let newClientDatabase = [newPhone:newClientProfile]

for (phone,client) in newClientDatabase {
  clientDatabase[phone] = client
}

09-03 23:54