2017-06-27 62 views
-1

「myfunction」是一個函數,我在不同的項目中使用過,我試圖修改它,所以我可以將myDic傳遞給它,並返回完整三個最高等級的信息(名字和姓氏)。Swift - 一個字典+數組作爲參數傳遞給一個函數

我是Swift新手,如何在函數中使用Dictionary>?

我應該使用「任何」嗎?

print()僅用於測試和調試。

任何幫助深表感謝

var myDic = Dictionary<String, Array<(firstName: String, lastName: String, email: String, phone: String, grade: Int)>>() 

myDic["one"] = [(firstName: "samer", lastName: "Unity", email:"[email protected]", phone:"34353453", grade:60)] 
myDic["two"] = [(firstName: "samer", lastName: "two", email:"[email protected]", phone:"666666", grade:72)] 
myDic["three"] = [(firstName:"bassouma", lastName: "battal", email:"[email protected]", phone:"55555", grade:99)] 
myDic["four"] = [(firstName: "alain", lastName: "b", email:"[email protected]", phone:"343535", grade:30)] 
myDic["five"] = [(firstName: "elias", lastName: "najm", email:"[email protected]", phone:"777777", grade:70)] 
myDic["six"] = [(firstName: "georges", lastName: "R", email:"[email protected]", phone:"888888", grade:90)] 


let t = myDic["two"]![0].grade 

print(t) 


enum Gender: String { 
    case male = "Male" 
    case female = "Female" 

} 

//Super class: 

class Person { 

    var firstName: String? 
    var lastName: String? 
    var gender: String? 


    init(firstName: String, lastName: String, gender: String) { 
     self.firstName = firstName 
     self.lastName = lastName 
     self.gender = gender 
    } 


func simpleDescription() -> String {   

return ("firstname: \(firstName!), lastname: \(lastName!) , gender: \(gender!).") } 


} 

let Samer = Person(firstName: "Samer", lastName: "kkkk", gender: Gender.male.rawValue) 


print(Samer.simpleDescription()) 



//Child class: 

class Student: Person { 

    var email: String? 
    var phone: String? 
    var grade: String? 


    func gradesConvert(grade: Int) -> String{ 

    switch grade { 
    case 0..<60: return "F" 
    case 60..<70: return "D" 
    case 70..<80: return "C" 
    case 80..<90: return "B" 
    case 90...100: return "A" 
    default: 
     return "Wrong grade" 
    } 

} 



func myfunction(items: Dictionary<String,Int>) -> (first: Dictionary<String,String>, second: Dictionary<String,String>, third: Dictionary<String,String>) { 

    let itemResult = items.sorted { (first: (key: String, value: Int), second: (key: String, value: Int)) -> Bool in 
     return first.value > second.value 
    } 

    var firstDic : [String:String] = [:] 
    var seconDic : [String:String] = [:] 
    var thirdDic : [String:String] = [:] 

    firstDic[itemResult[0].key] = gradesConvert(grade: itemResult[0].value) 
    seconDic[itemResult[1].key] = gradesConvert(grade: itemResult[1].value) 
    thirdDic[itemResult[2].key] = gradesConvert(grade: itemResult[2].value) 

    return (firstDic,seconDic, thirdDic) 
} 


     init(firstName: String, lastName: String, gender: String, email: String, phone: String, grade:String) { 

     super.init(firstName: firstName, lastName: lastName, gender: gender) 
self.email = email 
self.phone = phone 
self.grade = grade 

} 

override func simpleDescription() -> String {   


return ("firstname: \(firstName!), lastname: \(lastName!) , gender: \(gender!), email:\(email!), phone:\(phone!), grade: \(grade!) .") } 


} 



let Samer2 = Student(firstName: "Samer", lastName: "kkkk", gender: Gender.male.rawValue, email:"[email protected]", phone:"1234556", grade: "C") 


print(Samer2.simpleDescription()) 

回答

1

這是你的代碼從一個操場,剝奪其要領的適應。

爲了便於閱讀,我將使用幾條typealias語句。我不知道爲什麼你需要每一個字典條目是一個數組,但我現在就要去做。 :)

typealias StudentRecord = Array<(firstName: String, lastName: String, email: String, phone: String, grade: Int)> 
typealias StudentDictionary = Dictionary<String, StudentRecord> 

在數據設置或您的實用功能基本上沒有改變。

var myDic = StudentDictionary() 

myDic["one"] = [(firstName: "samer", lastName: "Unity", email:"[email protected]", phone:"34353453", grade:60)] 
myDic["two"] = [(firstName: "samer", lastName: "two", email:"[email protected]", phone:"666666", grade:72)] 
myDic["three"] = [(firstName:"bassouma", lastName: "battal", email:"[email protected]", phone:"55555", grade:99)] 
myDic["four"] = [(firstName: "alain", lastName: "b", email:"[email protected]", phone:"343535", grade:30)] 
myDic["five"] = [(firstName: "elias", lastName: "najm", email:"[email protected]", phone:"777777", grade:70)] 
myDic["six"] = [(firstName: "georges", lastName: "R", email:"[email protected]", phone:"888888", grade:90)] 

func gradesConvert(grade: Int) -> String { 

    switch grade { 
    case 0..<60: return "F" 
    case 60..<70: return "D" 
    case 70..<80: return "C" 
    case 80..<90: return "B" 
    case 90...100: return "A" 
    default: 
     return "Wrong grade" 
    } 
} 

現在我們需要在正確的的字典,這是比較容易得到權當這是它自己的類型傳遞。同樣,找到正確的成績價值路徑變得更簡單。 (同樣,數組是怪異。如果真的有必要,那麼[0]必須改變一些數字是有道理的上下文中)。

func myfunction(items: StudentDictionary) -> (first: Dictionary<String,String>, second: Dictionary<String,String>, third: Dictionary<String,String>) { 

    let itemResult = items.sorted { (first: (key: String, value: StudentRecord), second: (key: String, value: StudentRecord)) -> Bool in 
     return first.value[0].grade > second.value[0].grade 
    } 

    var firstDic : [String:String] = [:] 
    var seconDic : [String:String] = [:] 
    var thirdDic : [String:String] = [:] 

    firstDic[itemResult[0].key] = gradesConvert(grade: itemResult[0].value[0].grade) 
    seconDic[itemResult[1].key] = gradesConvert(grade: itemResult[1].value[0].grade) 
    thirdDic[itemResult[2].key] = gradesConvert(grade: itemResult[2].value[0].grade) 

    return (firstDic, seconDic, thirdDic) 
} 

只是要行使功能。

​​
+0

的例子缺少一些背景,但我想說,StudentDictionary實際上是'[字符串:[StudentTuple]' – nathan

+0

@nathan我沒有在數據結構的有效性任何信心可言,除非有在排序頂層元素時選擇使用哪個元組的一些缺失規則。 –

+0

'myDic.values.reduce([],+)。sorted {$ 0.grade> $ 1.grade} .prefix(upTo:3)'?考慮當元組數組有多個值時的情況 – nathan

相關問題