2016-02-28 132 views
0

我無法編碼一個看似簡單的任務。我想將新的客戶端配置文件數據添加到客戶端配置文件字典(clientDatabase),但不斷收到錯誤 - 似乎無法追加 - error:type'(String:clientProfile)'的值沒有成員'append'底部代碼)SWIFT將數據附加到字典中

您可以提供的任何見解都非常感謝。

感謝, 比爾

//: 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' 

回答

2

兩件事情。首先clientDatabase是一個沒有append的字典,你必須遍歷另一個字典並將其元素插入clientDatabase

另一個問題是clientDatabasenewClientDatabase不是相同的類型。第一個是​​,第二個是[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]() 

你正在創建一個struct只是包含一組值,當你已經有了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 
} 
+0

感謝Kenneth,感謝您的快速回復。我嘗試了代碼,它做我需要的。並感謝您的其他見解。 – user3784858