Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


In Swift enumerations, what’s the difference between raw values and associated values?



In Swift enumerations, what’s the difference between raw values and associated values?..

Answer / iosraj

Raw values are used to associate constant (literal) values to enum cases. The value type is part of the enum type, and each enum case must specify a unique raw value (duplicate values are not allowed).

The following example shows an enum with raw values of type Int:

enum IntEnum : Int {

case ONE = 1

case TWO = 2

case THREE = 3

}

An enum value can be converted to its raw value by using the rawValue property:

var enumVar: IntEnum = IntEnum.TWO

var rawValue: Int = enumVar.rawValue

A raw value can be converted to an enum instance by using a dedicated initializer:

var enumVar: IntEnum? = IntEnum(rawValue: 1)

Associated values are used to associate arbitrary data to a specific enum case. Each enum case can have zero or more associated values, declared as a tuple in the case definition:

enum AssociatedEnum {

case EMPTY

case WITH_INT(value: Int)

case WITH_TUPLE(value: Int, text: String, data: [Float])

}

Whereas the type(s) associated to a case are part of the enum declaration, the associated value(s) are instance specific, meaning that an enum case can have different associated values for different enum instances.

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Apple iOS Swift Interview Questions

What are the advantages of swift?

0 Answers  


Is swift worth learning 2019?

0 Answers  


What language is similar to swift?

0 Answers  


The String struct doesn’t provide a count or length property or method to count the number of characters it contains. Instead a global countElements<T>() function is available. When applied to strings, what’s the complexity of the countElements function: O(1) O(n) and why?

1 Answers  


What is difference between class and struct swift?

0 Answers  


Explain me what is swift?

0 Answers  


What is type aliasing in swift?

0 Answers  


How can you make a property optional in swift?

0 Answers  


How to add an element into an array?

0 Answers  


Is swift hard to learn?

0 Answers  


Explain mvc structure.

0 Answers  


What are the type of integers does swift have?

0 Answers  


Categories