Skip to main content

ad

Learning basics of (Enumerations) enums using swift

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.If you are familiar with C, you will know that C enumerations assign related names to a set of integer values. Enumerations in Swift are much more flexible, and do not have to provide a value for each case of the enumeration. If a value (known as a “raw” value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.

Enumeration Syntax

You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:


  1. enum SomeEnumeration {
  2. // enumeration definition goes here
  3. }

Here’s an example for the four main points of a compass:

  1. enum CompassPoint {
  2. case north
  3. case south
  4. case east
  5. case west
  6. }

The values defined in an enumeration (such as northsoutheast, and west) are its enumeration cases. You use the case keyword to introduce new enumeration cases.


Multiple cases can appear on a single line, separated by commas:
  1. enum Planet {
  2. case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. }
Each enumeration definition defines a brand new type. Like other types in Swift, their names (such as CompassPoint and Planet) should start with a capital letter. Give enumeration
types singular rather than plural names, so that they read as self-evident:

  1. var directionToHead = CompassPoint.west

The type of directionToHead is inferred when it is initialized with one of the possible values of CompassPoint. Once directionToHead is declared as a CompassPoint, you can set it to a different CompassPoint value using a shorter dot syntax:
  1. directionToHead = .east
The type of directionToHead is already known, and so you can drop the type when setting its value. This makes for highly readable code when working with explicitly typed enumeration values.

You can match individual enumeration values with a switch statement:

  1. directionToHead = .south
  2. switch directionToHead {
  3. case .north:
  4. print("Lots of planets have a north")
  5. case .south:
  6. print("Watch out for penguins")
  7. case .east:
  8. print("Where the sun rises")
  9. case .west:
  10. print("Where the skies are blue")
  11. }
  12. // Prints "Watch out for penguins"

Try yourself by step by step tutorials


1. Creating playground project



2. See sample code




3. Your code should look like




Enum is very easy to implement and very useful in programming.












Comments

ads

Popular posts from this blog

Do you know about the Apple Wallet ? Now You can pay with apple wallet, How ? Learn with us.

Apple Wallet  (referred to as simply  Wallet ) is an application in  Apple 's iOS (previously known as Passbook  in iOS 6 to iOS 8) that allows users to store coupons, boarding passes, event tickets, store cards and, starting with iOS 8.1, credit cards, loyalty cards, and debit cards via  Apple  Pay. With Wallet, you can keep your credit, debit, and prepaid cards, store cards, boarding passes, movie tickets, coupons, rewards cards, and more in one place. Add passes: You can add passes to Wallet in several ways: Using Wallet-enabled apps With Mail or Messages Through a web browser Scanning a barcode Sharing through AirDrop From your Mac Tapping a Wallet notification that you got after paying with Apple Pay at a supported merchant   Use your passes Some passes automatically appear at the right time or place because they include information based on time or location. For example, when you arrive at the airport, you...

Apple iOS New Programming Language Swift Introduction.

The powerful programming language that is also easy to learn. Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast. Introducing  Swift 3 Swift 3  is a thorough refinement of the language and the API conventions for the frameworks you use every day. These improvements make the code you write even more natural, while ensuring your code is much more consistent moving forward. For example, select Foundation types such as the new  Date  type are easier to use and are much faster than previous releases, and the  Calendar  type uses enums to feel more at home within Swift. Open Source Swift 3 is the first major release developed in the open at Swift.org, with source code, a bug tracker, mailing li...

Working with Blocks in iOS | Objective-C | Importance of Blocks in iOS Programming.

An Objective-C class defines an object that combines data with related behavior. Sometimes, it makes sense just to represent a single task or unit of behavior, rather than a collection of methods. Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like  NSArray  or  NSDictionary . They also have the ability to capture values from the enclosing scope, making them similar to  closures  or  lambdas  in other programming languages. This chapter explains the syntax to declare and refer to blocks, and shows how to use blocks to simplify common tasks such as collection enumeration. For further information, see  Blocks Programming Topics . Here is the apple reference:  https://developer.apple.com/library/content/docu...