Online Swift Compiler

import Foundation import Glibc enum Gender:Int{ //定义枚举类型性别 case Male case Female static func >(p1:Gender,p2:Gender) -> Bool{ //定义> 方法判断性别大小 return p1.rawValue < p2.rawValue } } class Person{ //Person类 var firstName:String var lastName:String var age:Int var gender:Gender var fullName:String { //计算属性fullName get{ 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:20,gender:Gender.Male) } func description() -> String{ //定义函数使其直接用print输出 return "Name: \(fullName),age:\(age),gender:\(gender)" } //函数重载使两个Person类的实例对象可以用==和!=比较 static func ==(per1:Person,per2:Person) -> Bool{ return per1.fullName==per2.fullName } static func != (per1:Person,per2:Person) -> Bool{ return !(per1==per2) } } var person1 = Person(firstName:"张",lastName:"三",age:18,gender:Gender.Male) var person2 = Person(firstName:"李",lastName:"小明") print(person1.description()) print(person2.description()) print("person1等于person2?",person1 == person2) print("person1不等于person2?",person1 != person2) class Student:Person{ //派生类Student var stuNo:String //Student属性 init(firstName:String,lastName:String,age:Int,gender:Gender,stuNo:String){ //Student类的初始化 self.stuNo = stuNo super.init(firstName:firstName,lastName:lastName,age:age,gender:gender) } override func description() -> String{ //重写父类方法 return "\(super.description()),stuno:\(stuNo)" } } class Teacher:Person{ //派生类Teacher var title:String //Teacher属性 init(firstName:String,lastName:String,age:Int,gender:Gender,title:String){ //Teacher类的初始化 self.title = title super.init(firstName:firstName,lastName:lastName,age:age,gender:gender) } override func description() -> String{ //重写父类方法 return "\(super.description()),title:\(title)" } } var student1 = Student(firstName:"王",lastName:"心",age:21,gender:Gender.Female,stuNo:"16001") //print(student1.description()) var teacher1 = Teacher(firstName:"周",lastName:"军",age:43,gender:Gender.Male,title:"Math") //print(teacher1.description()) var student2 = Student(firstName:"文",lastName:"阳",age:17,gender:Gender.Male,stuNo:"16002") var student3 = Student(firstName:"赵",lastName:"婷婷",age:20,gender:Gender.Female,stuNo:"16003") var teacher2 = Teacher(firstName:"邓",lastName:"小利",age:42,gender:Gender.Male,title:"Math") //建立数组,存放三种对象 var peopleArray = [Person]() //将对象追加到数组里 peopleArray.append(person1) peopleArray.append(person2) peopleArray.append(student1) peopleArray.append(student2) peopleArray.append(student3) peopleArray.append(teacher1) peopleArray.append(teacher2) for person in peopleArray{ //遍历输出数组里的对象 print(person.description()) } //定义字典统计对象个数 var dictionary = ["Person":0,"Student":0,"Teacher":0] for people in peopleArray{ if people is Teacher{ dictionary["Teacher"]! += 1 }else if people is Student{ dictionary["Student"]! += 1 }else{ dictionary["Person"]! += 1 } } //输出字典内容 print("字典内容如下:") for (key,value) in dictionary{ print("\(key):\(value)个") } //按年龄age排序 print("年龄排序如下:") peopleArray.sort(by:{return $0.age<$1.age}) for person in peopleArray{ //遍历输出数组里的对象 print(person.description()) } //按fullName排序 print("fullName排序如下:") peopleArray.sort(by:{return $0.fullName<$1.fullName}) for person in peopleArray{ //遍历输出数组里的对象 print(person.description()) } //按gender+age排序 print("gender+age排序如下:") peopleArray.sort(by:{ if $0.gender > $1.gender{ return true }else if($0.gender == $1.gender){ return $0.age > $1.age //性别相同,按年龄排序 }else{ return false } }) for person in peopleArray{ //遍历输出数组里的对象 print(person.description()) }

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:

ShortcutDescription
⌘ + EnterRun the program
⌘ + SSave Project (Login Required)
⇧ + ⌘ + SSave As Project
⌘ + PNew Project
⌘ + GShare Project
⌘ + ZUndo Editing
⌘ + YRedo Editing
⌘ + ASelect All Text
⌘ + XCut Selected Text
⌘ + CCopy Selected Text
⌘ + VPaste Copied Text
⌘ + FSearch Text
⌘ + ⌥ + FReplace Text
ShortcutDescription
Ctrl + EnterRun the program
Ctrl + SSave Project
Shift + Ctrl + SSave As Project
Ctrl + GShare Project
Ctrl + ZUndo Editing
Ctrl + YRedo Editing
Ctrl + ASelect All Text
Ctrl + XCut Selected Text
Ctrl + CCopy Selected Text
Ctrl + VPaste Copied Text
Ctrl + FSearch Text
Ctrl + HReplace 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.
 Execute |  Beautify | Share
My Projects
Change Password
My Profile
Logout
Undo
Redo
Cut
Copy
Paste
Delete
Select All
Find
Find and Replace
Editor Theme
Crimson
Eclipse
Github
Solarized
Cobalt
krTheme
Monokai
Terminal
Textmate
Twilight
Vibrant Ink
Font Size
8px
9px
10px
11px
12px
13px
14px
15px
16px
17px
18px
20px
22px
24px
Tab Size
1
2
3
4
5
6
7
8
Show Invisible
Hide Invisible
Show Line Numbers
Hide Line Numbers
Ace Editor (Default)
Vim Editor
Emacs Editor
Open New Project
Save Project
Save As New Project
Share Project
Search Project
Online Java Compiler
Online Python Compiler
Online C++ Compiler
Online CSharp Compiler
Online C Compiler
Online PHP Compiler
Online R Compiler
Online NumPy Compiler
More Compilers