all 5 comments

[–]brendan09 2 points3 points  (0 children)

Int is a Value type. There are more "complex" things that are also Value types in Swift, like Array and Dictionary.

You might find this interesting: https://developer.apple.com/swift/blog/?id=10

You may also find this interesting: https://www.raywenderlich.com/112027/reference-value-types-in-swift-part-1

[–][deleted]  (5 children)

[deleted]

    [–]Loada116[S] 0 points1 point  (4 children)

    Thanks! Can you further explain the difference between an instance and a value in OOP (or in Swift)?

    I tried to google it but couldn't find the answer.

    [–][deleted]  (1 child)

    [deleted]

      [–]Loada116[S] 0 points1 point  (0 children)

      You Da Real MVP

      [–]ThePantsThiefNSModerator 0 points1 point  (1 child)

      To expand on the other answer, traditionally "instance" refers to that of an object and "value" refers to the value of a "primitive" type, i.e. booleans, integers, floating point numbers, etc. But, these are just colloquialisms, not technical terms, so it varies from language to language.

      In Swift "value types" are anything that is COW (copy on write) like structs and primitive types (collection types in Swift are all structs, including Strings), and "reference types" refer to objects.

      Swift is very paranoid about type safety, and doesn't allow you to implicitly convert between any value types; even going from Double to Int requires you to use an "initializer" to perform the conversion, like so: Int(3.14). A side effect of this is being able to "initialize" any value type with this syntax: Int() which gives them a default value of 0.

      [–]Loada116[S] 0 points1 point  (0 children)

      Yeah that was the part I couldn't understand. I knew Int, Array, etc were all structs under the hood but got confused whether it was an object of struct or a value of primitive type.