PretzelJesus
PretzelJesus

Reputation: 899

Crashing During Iteration Of NSArray With Swift On iOS

I am not quite sure why this is not working, but I have an array populated with various data. I have tested the amount of rows in the array by using this:

println("Total Count: " + "\(testArray.count)")

This returns 29 items. I have also verified that each key / value pair is filled by looking at the output from:

for result : AnyObject in testArray
{
    println(\"(result)")
}

However when I try and get it to print the output of any given value, only the first one prints and the app crashes on the second loop. I don't get an error in the console but I get the message:

"Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

in the code. Here is the full code block:

for result : AnyObject in testArray
{
    var testVariable = result.valueForKey("testColumn") as NSObject
    println(testVariable)
}

The app crashes on the second loop on the "var testVariable" line. What am I doing wrong?

Here is the console output from the first two rows of data from my array:

<NSManagedObject: 0xd16e940> (entity: Shows; id: 0xd16fb40 <x-coredata://2DADFB50-FB10-4345-9252-1AFFF3598BBD/Shows/p22> ; data: {
   showId = 2492;
   showImageFilePath = "/Users/Andy/Library/Developer/CoreSimulator/Devices/2944D5A5-1D0A-4C23-BA74-54FEA1C2BB2E/data/Containers/Data/Application/8F6B6E36-87EA-45F8-94A7-F3E5278B8604/Documents\\ShowImages\\2492-All Shook Up.jp";
   showIsCurrent = 1;
   showIsWatched = 0;
   showName = "All Shook Up";
   showUrl = "http://lv.houseseats.com/member/tickets/view/?showid=2492";
})

<NSManagedObject: 0xd16ecf0> (entity: Shows; id: 0xd16fb50 <x-coredata://2DADFB50-FB10-4345-9252-1AFFF3598BBD/Shows/p50> ; data: <fault>)

<NSManagedObject: 0xd16eec0> (entity: Shows; id: 0xd16fb60 <x-coredata://2DADFB50-FB10-4345-9252-1AFFF3598BBD/Shows/p24> ; data: {
   showId = 2719;
   showImageFilePath = "/Users/Andy/Library/Developer/CoreSimulator/Devices/2944D5A5-1D0A-4C23-BA74-54FEA1C2BB2E/data/Containers/Data/Application/8F6B6E36-87EA-45F8-94A7-F3E5278B8604/Documents\\ShowImages\\2719-Bonkerz Comedy ";
   showIsCurrent = 1;
   showIsWatched = 0;
   showName = "Bonkerz Comedy Club Presents: Rio Hillman";
   showUrl = "http://lv.houseseats.com/member/tickets/view/?showid=2719";
})

Also, this is my first time trying to code for iOS as well as posting on Stack Overflow, so please bear with me. Thanks.

Upvotes: 1

Views: 1042

Answers (1)

Mundi
Mundi

Reputation: 80273

From the log output, it seems you are using NSManagedObject. Why use AnyObject? Maybe it does not understand valueForKey. Try casting your array elements as what they are, NSManagedObjects.

Also, if the property is a string, cast it as String, not something else.

Upvotes: 1

Related Questions