简体   繁体   English

自定义TableViewCell类无法返回枚举值而不会崩溃

[英]Custom TableViewCell class cannot return an enum value without crashing

In the class that inherits my UITableViewController I have: 在继承我的UITableViewController的类中,我有:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as GoalTableViewCell

        cell.priority = goalsArray[indexPath.row].priority as Priority //this causes no problems.
        println(cell.priority.toRaw()) //the getter causes a crash.
        //println(goalsArray[indexPath.row].priority.toRaw()) this Does Work.
        return cell
    }

In the class that is GoalTableViewCell I have: 在GoalTableViewCell类中,我有:

var priority : Priority {
        get {
            if (self.priority == Priority.highPriority) {
                return Priority.highPriority
            } else if (self.priority == Priority.mediumPriority) {
                return Priority.mediumPriority
            } else if (self.priority == Priority.lowPriority) {
                return Priority.lowPriority
            } else {
                return Priority.defaultPriority
            }
        }

        set  {
            if (newValue == Priority.defaultPriority) {
                //action here.
            } else if (newValue == Priority.lowPriority) {
                //action here.
            } else if (newValue == Priority.mediumPriority) {
                //action here.
            } else if (newValue == Priority.highPriority) {
                //action here.
            }
        }
    }

The getter is not working, as the setter appears to execute fine. 吸气器无法正常工作,因为该吸气器似乎执行得很好。

The error I receive is the following: 我收到的错误如下:

"THREAD 1: EXC_BAD_ACCESS (code=2, address=0x7fff518d9ff8)" There is no text in the console window generated by this error and the application completely crashes and cannot continue execution beyond this point. “线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x7fff518d9ff8)”此错误生成的控制台窗口中没有文本,并且应用程序完全崩溃,无法继续执行此操作。

The line it crashes at is if (self.priority == Priority.highPriority) { 崩溃的行是if (self.priority == Priority.highPriority) {

I think I am misunderstanding something about the getter? 我想我对吸气剂有误解?

Also here is the enum: 这也是枚举:

enum Priority : Int {
    case defaultPriority = 0
    case lowPriority = 1
    case mediumPriority = 2
    case highPriority = 3
}

Edit: Actually I cannot confirm whether the setter is working properly either, as it incorrectly performs for my tableView. 编辑:实际上,我不能确认设置器是否正常工作,因为它对我的tableView执行不正确。 if I run 如果我跑

    set  {
        if (newValue == Priority.defaultPriority) {
            self.backgroundColor = UIColor.redColor
        } else if (newValue == Priority.lowPriority) {
            //action here.
        } else if (newValue == Priority.mediumPriority) {
            //action here.
        } else if (newValue == Priority.highPriority) {
            //action here.
        }
    }
}

It actually incorrectly displays the cells consistently and sometimes they all become red when that should not be the case (as I scroll). 实际上,它会错误地一致地显示单元格,有时情况并非如此(当我滚动时),它们全部变为红色。 I am still trying to determine what is wrong. 我仍在尝试确定问题所在。

You're crashing because you've got an infinite loop in your getter. 您崩溃是因为您的吸气剂中存在无限循环。 Swift does not automatically create an ivar for your computed properties like Objective-C did. Swift不会像Objective-C那样为您的计算属性自动创建一个ivar。 So, when you call self.priority here: 因此,当您在此处调用self.priority

var priority : Priority {
        get {
            if (self.priority == Priority.highPriority) {

you're actually just calling your getter again. 您实际上只是在再次打电话给您的吸气剂。

You can easily solve this by simply creating your own ivar and using it in your getting/setter: 您只需创建自己的ivar并在获取/设置中使用它即可轻松解决此问题:

private var _priority: Priority = Priority.defaultPriority
var priority : Priority {
    get {
        if (_priority == Priority.highPriority) {
            return Priority.highPriority
        } else if (_priority == Priority.mediumPriority) {
            return Priority.mediumPriority
        } else if (_priority == Priority.lowPriority) {
            return Priority.lowPriority
        } else {
            return Priority.defaultPriority
        }
    }

    set  {
        _priority = newValue
        if (newValue == Priority.defaultPriority) {
            //action here.
        } else if (newValue == Priority.lowPriority) {
            //action here.
        } else if (newValue == Priority.mediumPriority) {
            //action here.
        } else if (newValue == Priority.highPriority) {
            //action here.
        }
    }
}

That being said, I'm not sure why you've got that if / else-if in your getter; 话虽这么说,但我不确定您为什么要在您的吸气剂中得到if / else-if simply returning _priority should do exactly the same thing: 简单地返回_priority应该做完全一样的事情:

get {
    return _priority
}

You can actually simplify things even further by using property observers on a standard (ie non-computed) property: 实际上,您可以通过在标准(即非计算)属性上使用属性观察器来进一步简化事情:

var priority : Priority {
    didSet {
        if (self.priority == Priority.defaultPriority) {
            //action here.
        } else if (self.priority == Priority.lowPriority) {
            //action here.
        } else if (self.priority == Priority.mediumPriority) {
            //action here.
        } else if (self.priority == Priority.highPriority) {
            //action here.
        }
    }
}

Note: Doing it that way, you don't need the _priority property anymore since you're not implementing a customer getter and setter. 注意:这样,您就不再需要_priority属性,因为您没有实现客户getter和setter。

And, just for completeness, I'd probably use a switch statement instead of an if / else-if : 而且,为了完整起见,我可能会使用switch语句而不是if / else-if

var priority: Priority {
    didSet {
        switch self.priority {
        case .lowPriority:
            // action here
        case .mediumPriority:
            // action here
        case .highPriority:
            // action here
        case .defaultPriority:
            // action here
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM