importFoundationenumMajor{caseAcaseBcaseCcaseD}structStudent{varname:Stringvarmajor:Major}letstudentList:[Student]=[Student(name:"Alice",major:.A),Student(name:"Bob",major:.B),Student(name:"Carl",major:.C),Student(name:"David",major:.D),Student(name:"Eric",major:.A),Student(name:"Fred",major:.D),Student(name:"Gavin",major:.C),Student(name:"Henry",major:.D),Student(name:"Ivy",major:.B),Student(name:"Janet",major:.D),Student(name:"Kathy",major:.A)]// 题目:// 统计studentList中各个专业的同学名字和总数目,并按照你倾向的方式打印。// TODO
importFoundation// 1structLog:CustomStringConvertible{// 3publicvartime:String// 4publicvarlog:String// 5init(filePath:String=#file,line:Int=#line,column:Int=#column,funcName:String=#function){// 7letfileName=(filePathasNSString).lastPathComponent// 8letformatter=DateFormatter()// 10formatter.dateStyle=.none// 11formatter.timeStyle=.medium// 12time=formatter.string(from:Date())// 13log="\(fileName)(\(line),\(column)) \(funcName)"// 15}publicvardescription:String{// 18return"[\(time)\(log)]\n\t"// 19}publicvarstring:String{// 22description// 23}// 24publicvarerror:String{// 26"[ERROR] "+description// 27}}funcMyPrint(){// 31print(Log().string+"Hello, world!")// 32print(Log().error+"Bye, world.")// 33}MyPrint()// 36// Literal Type Value//// #file String The name of the file in which it appears.// #line Int The line number on which it appears.// #column Int The column number in which it begins.// #function String The name of the declaration in which it appears.
importFoundationenumMajor{caseAcaseBcaseCcaseD}structStudent{varname:Stringvarmajor:Major}letstudentList:[Student]=[Student(name:"Alice",major:.A),Student(name:"Bob",major:.B),Student(name:"Carl",major:.C),Student(name:"David",major:.D),Student(name:"Eric",major:.A),Student(name:"Fred",major:.D),Student(name:"Gavin",major:.C),Student(name:"Henry",major:.D),Student(name:"Ivy",major:.B),Student(name:"Janet",major:.D),Student(name:"Kathy",major:.A)]// 题目:// 统计studentList中各个专业的同学名字和总数目,并按照你倾向的方式打印。// TODO
importFoundation// 1structLog:CustomStringConvertible{// 3publicvartime:String// 4publicvarlog:String// 5init(filePath:String=#file,line:Int=#line,column:Int=#column,funcName:String=#function){// 7letfileName=(filePathasNSString).lastPathComponent// 8letformatter=DateFormatter()// 10formatter.dateStyle=.none// 11formatter.timeStyle=.medium// 12time=formatter.string(from:Date())// 13log="\(fileName)(\(line),\(column)) \(funcName)"// 15}publicvardescription:String{// 18return"[\(time)\(log)]\n\t"// 19}publicvarstring:String{// 22description// 23}// 24publicvarerror:String{// 26"[ERROR] "+description// 27}}funcMyPrint(){// 31print(Log().string+"Hello, world!")// 32print(Log().error+"Bye, world.")// 33}MyPrint()// 36// Literal Type Value//// #file String The name of the file in which it appears.// #line Int The line number on which it appears.// #column Int The column number in which it begins.// #function String The name of the declaration in which it appears.
〇 该程序的运行结果是(
Text Only
12345
[23:51:18 main.swift(32,14) MyPrint()]
Hello, world!
[ERROR] [23:51:18 main.swift(33,14) MyPrint()]
Bye, world.
Program ended with exit code: 0