Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Enums Associated Values

i can't understand why?

Hello every one . . .

enum Proposition {

    case True(String),
   False (String)

}

let proposition = Proposition.True("it is a proposition")

switch proposition {
case .True(let T):
    println(T)
case .False(let F):
    println(F)
}

I want to ask about this part of raw value :

let proposition = Proposition.True("it is a proposition")

why we can't edit two strings one to tell if it a Proposition and other one to tell if it's not ?!

One other thing, why we have to assign a let or var in this part:

case.True(let T)
Tommy Choe
Tommy Choe
38,156 Points

Hey Nour,

I'm not sure what you're asking when you say "why we can't edit two strings one to tell if it a Proposition and other one to tell if it's not ?!"

4 Answers

i meant why we can't write some thing like that : let proposition = Proposition.True("it is a proposition"),Proposition.False("it's not a prposition") !!

Tommy Choe
Tommy Choe
38,156 Points

Are you sure that you don't want to use raw value instead of associated value? That way you can just get the rawValue property once you set .True or .False. Let me show you what I mean.

enum Proposition: String {
    case True = "it is a proposition",
    False = "it is not a proposition"
}
let proposition = Proposition.True

switch proposition {
case .True:
    print(proposition.rawValue)
case .False:
    print(proposition.rawValue)
}
// prints out: it is a proposition

Let me know if you're trying to do something else.

thank you tommy choe :)

Tommy Choe
Tommy Choe
38,156 Points

No problem Nour. Happy coding!

Ghaith Ali
Ghaith Ali
3,134 Points

Noor, I am not sure if my answer will help, but I think you are mixing between enums raw values and enum associated vaules. A raw value is when you give the value during the creating of the enum:

enum Size: Int {
case Big = 1 , Medium = 2, Small = 3
}
let biggestSize = Size(rawValue: 1)

while an associated value is a value assigned when creating an instance of the enum, ex:

enum Size {
case Big (Int) , Medium (Int), Small(Int)
}
let biggestSize = Size.Big(41)

notice that in the case of associated values, we give the value when creating an instance of the enum.

regarding the other question: we assign let or var to the line:

case.True(let T)

to extract the associated value we parsed before. hope my explaining helped.

Ghaith Ali شكراً لك غيث ، اجابتك على السؤال الثاني كانت افية :)

Ghaith Ali
Ghaith Ali
3,134 Points

العفو زين كدرت اساعدك شوية :)