Problems checking to see if a value exists in Swift 2

155 views Asked by At

I'm writing a quiz app and there is a table view to add a subject (The name is saved to core data) and when you select an index path it passes the subject to a detail view controller (this works just fine on its own) but what I'm having trouble with is checking to see if any cards exists (the subject entity has an NSOrderedSet of "Cards"). I keep getting a crash on my two attempts, I've done this in swift before with relationships and tableViews and it's always been fine so I'm not sure what the problem is here. Thank you for the help like always!

My first attempt, although it says "catch block is unreachable because no errors are thrown in do block", it crashes with "Bad Instruction" on the line after "do"

    do {
        if let firstCard = self.subject?.cards![0] as? Card {
            self.currentCard = firstCard
        }
    } catch {


    }

My second attempt, it crashes on the first line

    if let firstCard = self.subject?.cards![0] as? Card {
        self.currentCard = firstCard
    }

My third attempt

    if self.subject!.cards != nil {

        self.currentCard = self.subject!.cards![0] as! Card
    }

My fourth attempt, unwrapping both the subject property and subject, it not rings out self.subject.cards but still crashes

    if let firstCard = self.subject!.cards?[0] as? Card {
        self.currentCard = firstCard
    }

Where the properties are declared

var draggableView = DraggableView!()
var subject : Subject?
var currentCard : Card?
var cardArray = [Card]()

The update with subject method (works perfectly, but put it here just for reference), the subject is passed from another class and this method called at the top of view did load.

func updateWithSubject(subject: Subject) {

    if let subject = self.subject {
        self.subject = subject
    }
}
2

There are 2 answers

0
dfrib On BEST ANSWER

In all four of your attempts, you do a "forced" unwrapping (!) at some point, which is to be generally avoided. Also, you attempt to explicitly accessing index 0 in your NSOrderedSet; if the set is empty, accessing [0] will yield a runtime exception.

Instead, you could use the .array representation of the NSOrderedSet and use the .first (optional) property of array for a safe access test:

if let firstCard = self.subject?.cards?.array.first ...
2
mango On

I think there's some awkwardness going on in your updateWithSubject method. You're checking if there's a subject already initialized before assigning to it. If that's in your viewDidLoad and only there, the assignment will never happen. The forced unwrapping that follow will surely fail after.