-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())
的例子缺少一些背景,但我想說,StudentDictionary實際上是'[字符串:[StudentTuple]' – nathan
@nathan我沒有在數據結構的有效性任何信心可言,除非有在排序頂層元素時選擇使用哪個元組的一些缺失規則。 –
'myDic.values.reduce([],+)。sorted {$ 0.grade> $ 1.grade} .prefix(upTo:3)'?考慮當元組數組有多個值時的情況 – nathan