Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make switch case of enum case (swift)

I got this code from ObjC. I want to convert it to Swift, however, I am having difficulties doing so...

ObjC code :

navgivet.h

typedef NS_ENUM(NSInteger, BB3Photo) {
kirkenType = 10 ,
festenType = 20 ,
praestType = 30
};
@property (nonatomic, assign) BB3Photo selectedPhotoType;

navgivet.m

- (IBAction)changeImage:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
    UIButton *button = sender;
    _selectedPhotoType = button.tag;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Vælg Billed"
                                                   delegate:self
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:@"Vælg fra Biblioteket", @"Vælg Kamera", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:[self.view window]];

}

switch (_selectedPhotoType) {
    case kirkenType: {
}break;
        case festenType: {
}break;
    case praestType: {
}break;
    default: 
       break;

Here's my swift code in this attempt

 enum BBPhoto1: Int {
    case kommunen = 10
    case sagsbehandler = 20
    case festen = 30
}
@IBAction func changeImage(sender: AnyObject){
    if sender .isKindOfClass(UIButton){
        let button: UIButton = sender as UIButton
        selectedPhoto = BBPhoto1.fromRaw(button.tag)
    }

    let actionSheet = UIActionSheet(title: "Billeder", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil, otherButtonTitles: "Vælg fra Biblioteket", "Vælg Kamera")
    actionSheet.showInView(self.view)

}
var selectedPhoto: BBPhoto1?

switch (selectedPhoto) {
        case kommunen {

        }

    case sagsbehandler{

        }
    }

but I get errors : "Use of unresolved identifier kommunen" and same messege but with Sagsbehandler.

How do I make it work ?

like image 319
KennyVB Avatar asked Oct 04 '14 07:10

KennyVB


1 Answers

There are 3 problems in your code.

The first is that selectedPhoto is declared as optional, so you must unwrap it before using in a switch statement - for example using optional binding.

The second problem is that the syntax you're using is incorrect. In each case you have to specify the full name (including the type), followed by a colon:

case BBPhoto1.kommunen:
    // statements

but since the type can be inferred by the variable type used in the switch, you can ignore the enum type, but not the dot:

case .kommunen:
    // statements

Last, in swift a switch statements require that all cases are handled either explicitly (3 in your case) or using a default case covering all cases non explicitly handled in the switch.

A working version of your code would look like:

enum BBPhoto1: Int {
    case kommunen = 10
    case sagsbehandler = 20
    case festen = 30
}

var selectedPhoto: BBPhoto1?

if let selectedPhoto = selectedPhoto {
    switch (selectedPhoto) {
    case .kommunen:
        println(selectedPhoto.toRaw())

    case .sagsbehandler:
        println(selectedPhoto.toRaw())

    default:
        println("none")
    }
}

Note that, differently from other languages, the code in each case doesn't automatically fallthrough to the next case, so the break statement is not required - the only use case for it is when a case has no statement (a case with no statement is an error in swift), and in that case the break just acts as a placeholder and its meaning is 'do nothing'.

Suggested reading: Conditional Statements

like image 112
Antonio Avatar answered Sep 20 '22 11:09

Antonio