Loading the player...

Classes vs. Structures in Swift 4, Xcode 9, and iOS 11 - raywenderlich.com

  • Learn about the differences between classes and structures in Swift, and when you should use which.nnWatch the full course here:nvideos.raywenderlich.com/courses/90-programming-in-swift/lessons/1nn----nAbout www.raywenderlich.com:nnraywenderlich.com is a website focused on developing high quality programming tutorials. Our goal is to take the coolest and most challenging topics and make them easy for everyone to learn – so we can all make amazing apps.nnWe are also focused on developing a strong community. Our goal is to help each other reach our dreams through friendship and cooperation. As you can see below, a bunch of us have joined forces to make this happen: authors, editors, subject matter experts, app reviewers, and most importantly our amazing readers!n---nnClasses and structures are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, and functions.nnUnlike other programming languages, Swift does not require you to create separate interface and implementation files for custom classes and structures. In Swift, you define a class or a structure in a single file, and the external interface to that class or structure is automatically made available for other code to use.nnNote: An instance of a class is traditionally known as an object. However, Swift classes and structures are much closer in functionality than in other languages, and much of this chapter describes functionality that can apply to instances of either a class or a structure type. Because of this, the more general term instance is used.nnClasses and structures in Swift have many things in common. Both can:nn Define properties to store valuesnn Define methods to provide functionalitynn Define subscripts to provide access to their values using subscript syntaxnn Define initializers to set up their initial statenn Be extended to expand their functionality beyond a default implementationnn Conform to protocols to provide standard functionality of a certain kindnnClasses have additional capabilities that structures do not:nn Inheritance enables one class to inherit the characteristics of another.nn Type casting enables you to check and interpret the type of a class instance at runtime.nn Deinitializers enable an instance of a class to free up any resources it has assigned.nn Reference counting allows more than one reference to a class instance.nnClasses and structures have a similar definition syntax. You introduce classes with the class keyword and structures with the struct keyword. nnStructures and Enumerations Are Value TypesnnA value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.nnYou’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.nnAll structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they are passed around in your code.nnClasses Are Reference TypesnnUnlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.nnChoosing Between Classes and StructuresnnYou can use both classes and structures to define custom data types to use as the building blocks of your program’s code.nnHowever, structure instances are always passed by value, and class instances are always passed by reference. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.nnAs a general guideline, consider creating a structure when one or more of these conditions apply:nn The structure’s primary purpose is to encapsulate a few relatively simple data values.nn It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.nn Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.nn The structure does not need to inherit properties or behavior from another existing type.

    Category : Наука и техника

    #classes#structures#swift#4#xcode#9#ios#11#raywenderlich

    0 Comments and 0 replies
arrow_drop_up