import Foundation
import Glibc
//枚举类Gender的定义
enum Gender:Int {
case female,male
// static func < (gender1: Gender, gender2: Gender) ->Bool{
// return gender1.rawValue < gender2.rawValue
// }
}
//Person类的定义
class Person {
//Person类属性定义
var firstName:String
var lastName:String
var age:Int
var gender:Gender
var fullName:String { return firstName + " " + lastName }
//构造函数
init (firstName:String, lastName:String, age:Int, gender:Gender){
self.firstName = firstName
self.lastName = lastName
self.age = age
self.gender = gender
}
//便利构造函数
convenience init (firstName:String, lastName:String) {
self.init (firstName: firstName, lastName: lastName,age: 19, gender: Gender.male)
}
//信息显示
func printInfo() -> String {
return "Name: \(fullName) Age: \(age) Gender: \(gender)"
}
//==和!=方法,用于比较两个Person类实例是否相同
static func == (person1:Person, person2:Person) -> Bool {
return (person1.fullName == person2.fullName && person1.age == person2.age && person1.gender == person2.gender)
}
static func != (person1:Person, person2:Person) -> Bool {
return (person1.fullName != person2.fullName || person1.age != person2.age || person1.gender != person2.gender)
}
}
//Person类的派生类Teacher类
class Teacher: Person {
//Teacher类新增属性title的定义
var title: String
//构造函数
init (firstName:String, lastName:String, age:Int, gender:Gender, title: String){
self.title = title
super.init(firstName: firstName, lastName: lastName, age: age, gender: gender)
}
//显示信息
override func printInfo() -> String {
return super.printInfo() + " Title: \(title)"
}
}
//Person类的派生类Student类
class Student: Person {
//Student类新增属性stuNo的定义
var stuNo: String
//构造函数
init (firstName:String, lastName:String, age:Int, gender:Gender, stuNo: String){
self.stuNo = stuNo
super.init(firstName: firstName, lastName: lastName, age: age, gender: gender)
}
//显示信息
override func printInfo() -> String {
return super.printInfo() + " StudentNo: \(stuNo)"
}
}
//多个类实例的构建
//Person类
var person1 = Person(firstName: "Finn", lastName: "Wolfhard", age: 15 , gender: Gender.male)
print(person1.printInfo())
var person2 = Person(firstName: "Tarjei", lastName: "Sandvikmoe")
print(person2.printInfo())
var person3 = Person(firstName: "Tao", lastName: "Lu", age: 30 , gender: Gender.male)
//Teacher类
var teacher1 = Teacher(firstName: "John", lastName: "Keating", age: 35, gender: Gender.male, title: "principal")
var teacher2 = Teacher(firstName: "Graham", lastName: "Coxon", age: 49, gender: Gender.male, title: "guitar_teacher")
print(teacher1.printInfo())
//Student类
var studentA = Student(firstName: "Maria", lastName: "Ray", age: 18, gender: Gender.female, stuNo: "2018010101")
print(studentA.printInfo())
var studentB = Student(firstName: "James", lastName: "Mcavoy", age: 19, gender: Gender.male, stuNo: "2018010102")
//判断是否为同一人
var isEqual = (person1 == person2)
var isNotEqual = (person1 != person2)
print("Are they the same person?")
if isEqual || !isNotEqual{
print("Yes, they are the same person")
}
else{
print("No, they are different persons")
}
//创建人群数组
var people = [person1, person2, person3, teacher1, teacher2, studentA, studentB]
var result = [
"pTotalNo": 0,
"tTotalNo": 0,
"sTotalNo": 0
]
//创建类别统计字典
for i in people {
result["pTotalNo"]? += 1
if i is Teacher {
result["tTotalNo"]? += 1
}
else if i is Student {
result["sTotalNo"]? += 1
}
}
print(result)
//对数组排序并输出
print("Sorting by age")
people.sort(){$0.age < $1.age}
for i in people {
print(i.printInfo())
}
print("Sorting by fullName")
people.sort(){$0.fullName < $1.fullName}
for i in people {
print(i.printInfo())
}
print("Sorting by gender and age")
people.sort(){
if($0.gender.rawValue < $1.gender.rawValue){
return ($0.gender.rawValue < $1.gender.rawValue)
}else{
return $0.age < $1.age
}
}
for i in people {
print(i.printInfo())
}
About Online Swift Compiler
Try our Online Swift Compiler (Version Swift v5.7.3) to Edit, Run, and Share your Swift Code directly from your browser. This online development environment provides you the latest version Swift v5.7.3.
How to use Online Swift Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Swift Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Swift Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Swift Compiler: Save and Share Swift Code (Project)
Save Swift Project Online
You can save your Swift Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Swift Project Online
You can use this feature to share your Swift Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Swift Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Swift Compiler
There are several benefits of using the Online Swift Compiler to run your Swift code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.