​Swift is just a strong and spontaneous programming language for macOS, iOS, watchOS and tvOS....

Page 109

Swift

var studDetails = Student.Name("Swift") var studMarks = Student.Mark(98,97,95) switch studMarks { case .Name(let studName): println("Student name is: \(studName).") case .Mark(let Mark1, let Mark2, let Mark3): println("Student Marks are: \(Mark1),\(Mark2),\(Mark3).") default: println("Nothing") } When we run the above program using playground, we get the following result: Swift 98 97 95 Consider for example to access the students name and marks secured in three subjects enumeration name is declared as student and the members present in enum class are name which belongs to string datatype, marks are represented as mark1, mark2 and mark3 of datatype Integer. To access either the student name or marks they have scored var studDetails = Student.Name("Swift") var studMarks = Student.Mark(98,97,95) Now, the switch case will print student name if that case block is executed otherwise it will print the marks secured by the student. If both the conditions fail, the default block will be executed.

Enum with Raw Values Raw values can be strings, characters, or any of the integer or floating-point number types. Each raw value must be unique within its enumeration declaration. When integers are used for raw values, they auto-increment if no value is specified for some of the enumeration members. enum Month: Int { case January = 1, February, March, April, May, June, July, August, September, October, November, December }

97


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.