【问题标题】:Swift - enum with button.tag?Swift - 带有 button.tag 的枚举?
【发布时间】:2014-09-29 16:50:24
【问题描述】:

我在 ObjC 中有这段代码 我想要或试图将其转换为 swift

typedef NS_ENUM(NSInteger, BB3Photo) {
kirkenType = 10 ,
festenType = 20 ,
praestType = 30
};

@property (nonatomic, assign) BB3Photo selectedPhotoType;


- (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]];

}

这是我用它做的

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

var selectedPhoto = BBPhoto1.self

@IBAction func changeImage(sender: AnyObject){
    if sender .isKindOfClass(UIButton){
        let button: UIButton = sender as UIButton
        selectedPHoto = (sender as UIButton).tag as BBPhoto1 // doesent work "cannot assign that result as expression"
        selectedPHoto = button.tag // doesnt work either "cannot assign that result as expression"
        self.selectedPhoto = BBPhoto1.fromRaw((sender as UIButton).tag) // nope "cannot convert the expressions type () to type UIButton"

    }
}

我希望能够有一个带有按钮标签的 switch 语句到相同的功能但在代码中不同

【问题讨论】:

  • 为什么不直接@IBAction func changeImage(sender: UIButton) {...?您是否期待其他类型的发件人

标签: objective-c swift enums ios8


【解决方案1】:

您想使用tag 作为BBPhoto1 枚举的原始值。您可以通过条件展开来做到这一点:

@IBAction func changeImage(sender: AnyObject){
    if let button = sender as UIButton {
        if let photoType = BBPhoto1.fromRaw(button.tag) {
            self.selectedPhoto = photoType
        }
    }
}

selectedPhoto 属性的声明也存在问题。应该是:

var selectedPhoto: BBPhoto1?

你现在拥有它的方式不是BBPhoto1 值,而是BBPhoto1 本身的类型


请注意,fromRaw 语法在 Xcode 6.1 中已更改为初始化程序:

@IBAction func changeImage(sender: AnyObject){
    if let button = sender as UIButton {
        if let photoType = BBPhoto1(rawValue: button.tag) {
            self.selectedPhoto = photoType
        }
    }
}

【讨论】:

  • BBPhoto1 不能转换为 BBPhoto1.Type
【解决方案2】:

怎么样:

@IBAction func changeImage(sender: AnyObject){
    if sender .isKindOfClass(UIButton){
        let button: UIButton = sender as UIButton
        selectedPHoto = BBPhoto1.fromRaw(button.tag)
    }
}

或(更短):

@IBAction func changeImage(sender: UIButton){
    selectedPHoto = BBPhoto1.fromRaw(sender.tag)
}

【讨论】:

  • 其中任何一个都不起作用,它说无法将表达式 type () 转换为 type
【解决方案3】:

好的,代码不一样;与验证文本字段一样,但在 Swift 4、iOS 11、xCode 9.2 中的原理相同。

enum SFT: Int {
    case StudentNameFieldTag
    case StudentIDFieldTag
    case StudentClassFieldTag
    case ChallengeCodeFieldTag
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.tag == SFT.StudentClassFieldTag.rawValue {
        let whitespaceSet = CharacterSet.whitespaces
        if let _ = string.rangeOfCharacter(from: whitespaceSet) {
            return false
        } else {
            return true
        }
    }
    return true
}

【讨论】:

    【解决方案4】:

    fromRaw() 方法返回一个可选项,因此您必须将您的属性声明为可选项:

    var selectedPHoto: BBPhoto1?
    

    并使用此代码:

    self.selectedPhoto = BBPhoto1.fromRaw((sender as UIButton).tag)
    

    或者,您可以打开 fromRaw 返回值:

    self.selectedPHoto = BBPhoto1.fromRaw((sender as UIButton).tag)!
    

    但是请注意,在这种情况下,如果原始值未映射到枚举,则会引发运行时异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2017-12-09
      相关资源
      最近更新 更多