What is the difference between Enum, Structs, Classes

So, I studied online and I cannot find simple definitions for these three. I know that An Enum and Struct can contain properties, initializers, and methods, and that both data structures are also passed by value, but this is pretty much it ...

I want to know what is the difference between these 3 (Enum, Structs, Classes)? in the simplest definitions for each of them?

+4
source share
3 answers

I think Chris Upgeon gives a fairly simple but excellent explanation on the topic at Treehouse:

Transfers

, String Int , , , - - .

let myString = "test"

if myString == "ttest" {
  // Doesn't execute because "ttest" is the value assigned to "myString"
}

, .

enum MyEnum: String {
  case Test = "test"
}

let enumValue = MyEnum.Test

if enumValue == MyEnum.Test {
  // Will execute because we can reassign the value of "MyEnum.Test" unless we do so within "MyEnum"
}

, MVC, Swift , , . MVC Swift.

- , . , UIView, , Controller - , , UIView- UITableView

, , , , - , .

struct Birthday {
  var day: Int
  var month: Int
  var year: Double
}

struct Person {
  var firstName: String
  var lastName: String
  var birthday: Birthday
  var phoneNumber: String
  var emailAddress: Int
}

Person, , Birthday , , , , , , .

, , . iOS , , , , .

- , , , , , , , , , , , , .


: . Swift (, CS, ), , - - .


, !

+7

Enum

" ". , (, , ).

enum StartuUpError : ErrorType{
    case ShowStoppingBug(bugid: Int)
    case TooManyDistractions
    case NotEnoughTime
    case NotEnoughFunding(shortFall: Int)
}

:

enum StartuUpError : ErrorType{
    case ShowStoppingBug(bugid: Int)
    case TooManyDistractions
    case NotEnoughTime
    case NotEnoughFunding(shortFall: Int)
}
class Startup {
    var funding = 0
    var completion = 0
    var burnRate = 10000
    var week = 0

    func raiseCapital(amount: Int){
        funding += amount
    }
    func completeWork(units: Int) throws{
        week += units
        funding -= burnRate

        if funding < 0{
           throw StartuUpError.NotEnoughFunding(shortFall: funding)
        }
        if completion >= 100{
            print("Congratulations! You've been achieved! ")
        }
    }

}
let myStrtup = Startup()
myStrtup.raiseCapital(25000)

do{
    let weeklyWOrk = 20
    for _ in 1...3{
        try myStrtup.completeWork(weeklyWOrk)
        print("At week \(myStrtup.week), tried to do \(weeklyWOrk) uits of work")
    }
}catch{
    if let error = error as? StartuUpError{
        switch error{
        case  .NotEnoughFunding(let shortFall):
            print("Ran out of funding! ShortFall:\(shortFall)")
        default:
            print("Some other problem")

        }
    }
}

Struct?

. , , , . , , ( )

struct slidingDoor : Door{
    var isLocked : Bool = false

    func open() {
        print("Whoooshh")
    }
    func close() {
        print("whooosh")
    }
    var description : String{
        return "Smooth sliding door"
    }
}

:

class BankValut : Door{
    var isLocked : Bool = true

    func open() {
        if isLocked{
          print("Locked !")
        }else{
            print("Clang!")
        }

    }
    func close() {
        print("Slamm")
        isLocked = true
    }
    func unlock(combination : String){
        isLocked = false
    }
    var description: String {
        if isLocked {
            return "A bank vault that is locked"
        } else {
            return "An unlocked bank vault"
        }
    }
}

let door1 = slidingDoor()
door1.open(

)

?

, . , ! , , , OO, , .

+1
  • ,

2. , (, ), ,

3.A , , .

Classes can be extended by inheritance. Methods within the class can be overloaded or implemented later.

More here

This is an older post explaining the difference between enum and struct:

0
source

Source: https://habr.com/ru/post/1627010/


All Articles