ERROR: Thread 1


#1

Building the following code will be successful however when running the code, I receive a thread error:

Below is my code:


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //Main property list array
        NSMutableArray *plist = [[NSMutableArray alloc] init];
        
        NSMutableDictionary *dictionary;
        
        //Create Entry 1
        dictionary = [NSMutableDictionary dictionary];
        [dictionary setObject:@"Eric"
                       forKey:@"Name"]; //String
        [dictionary setObject:[NSNumber numberWithInt:111223333]
                       forKey:@"SSN"]; //Integer
        [dictionary setObject:[NSDate date]
                       forKey:@"DOB"]; //Date
        [dictionary setObject:[NSNumber numberWithBool:YES]
                       forKey:@"Married"]; //Boolean
        [dictionary setObject:[NSNumber numberWithFloat:67165.32]
                       forKey:@"Salary"]; //Float
        [dictionary setObject:[NSArray arrayWithObjects:@"Australia",@"Mexico",@"Bahamas", nil] forKey:@"Countries Visited"]; //Array
        [dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Frank",@"Milly",@"John","@Jenny", nil] forKeys:[NSArray arrayWithObjects:@"Dad",@"Mom",@"BestFriend",@"Crush", nil]]
                       forKey:@"Important People"]; //Dictionary (with array for easy creation)
        [dictionary setObject:[NSData dataWithContentsOfFile:@"/tmp/eric.png"]
                       forKey:@"Picture"]; //Data
        
        //Add Entry 1 to PLIST
        [plist addObject:dictionary];
  
    }
    return 0;
}

Any help is appreciated, I am one chapter away from finally creating my first iOS application!


#2

I think it has something to do with arrays and dictionary that I put inside of my dictionary object that gets placed inside of the plist.


#3

Got it!

The error was with initializing and allocating the arrays and dictionaries that I placed inside of the dictionary. I had to pass different methods for it work so view the code to see what I did to fix it.

I probably wouldn’t have this problem, but for some reason I wanted to keep everything in one line. So instead of allocating and initializing the classes ahead of time, then assigning them to be used – I did it all in one line.

[code][dictionary setObject:[[NSArray alloc] initWithObjects:@“Australia”,@“Mexico”,@“Bahamas”, nil] forKey:@“Countries Visited”]; //Array

[dictionary setObject:[[NSDictionary alloc] initWithObjectsAndKeys:@“Frank”,@“Dad”,@“Milly”,@“Mom”,“John”,@“BestFriend”,@“Jenny”,@“Crush”, nil] forKey:@“Important People”]; //Dictionary (with array for easy creation)[/code]


#4

Happy New Year, Mr Forbes!

But you haven’t really found the cause of the problem.

The cause of the problem was a bit like Kit Versus Kat: that is, @Jenny versus @“Jenny”.

Compare:


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        //Main property list array
        NSMutableArray *plist = [[NSMutableArray alloc] init];
        
        NSMutableDictionary *dictionary;
        
        //Create Entry 1
        dictionary = [NSMutableDictionary dictionary];
        [dictionary setObject:@"Eric"
                       forKey:@"Name"]; //String
        [dictionary setObject:[NSNumber numberWithInt:111223333]
                       forKey:@"SSN"]; //Integer
        [dictionary setObject:[NSDate date]
                       forKey:@"DOB"]; //Date
        [dictionary setObject:[NSNumber numberWithBool:YES]
                       forKey:@"Married"]; //Boolean
        [dictionary setObject:[NSNumber numberWithFloat:67165.32]
                       forKey:@"Salary"]; //Float
        [dictionary setObject:[NSArray arrayWithObjects:@"Australia",@"Mexico",@"Bahamas", nil] forKey:@"Countries Visited"]; //Array
#if 0
        [dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Frank",@"Milly",@"John","@Jenny", nil] forKeys:[NSArray arrayWithObjects:@"Dad",@"Mom",@"BestFriend",@"Crush", nil]]
                       forKey:@"Important People"]; //Dictionary (with array for easy creation)
#else
        [dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Frank",@"Milly",@"John",@"Jenny", nil] forKeys:[NSArray arrayWithObjects:@"Dad",@"Mom",@"BestFriend",@"Crush", nil]]
                       forKey:@"Important People"]; //Dictionary (with array for easy creation)
#endif

        [dictionary setObject:[NSData dataWithContentsOfFile:@"/tmp/eric.png"]
                       forKey:@"Picture"]; //Data
        
        //Add Entry 1 to PLIST
        [plist addObject:dictionary];
        
    }
    return 0;
}

In Objective-C,@Jenny is not the same as @“Jenny”: @Jenny is a const char* string, whereas @“Jenny” is a NSString object.


#5

Gotta love typos, thank you for pointing that out. It’s all a learning process!

Happy New Year!