1

I read many posts regarding the same error message on SO, but none of those solutions works in my situation. So please read the code before recommend other posts. Thanks in advance.

I set exception break point, and found the problem resides in [self.countryNameDictionary setObject:array forKey:initial];

In AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,copy) NSMutableDictionary *countryNameDictionary;
@property (nonatomic,copy) NSMutableArray *countryNameInitials;
@end

In AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate     
@synthesize window = _window;
@synthesize countryNameDictionary = _countryNameDictionary;
@synthesize countryNameInitials = _countryNameInitials;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSMutableArray *permanentArray = [[NSMutableArray alloc]init];
    self.countryNameDictionary = [[NSMutableDictionary alloc]init];
    self.countryNameInitials = [[NSMutableArray alloc]init ];

    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"isFirstLaunch"];

    // run this code only once by creating and checking a user defaults key
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLaunch"] isEqualToString:@"YES"])
    {
        // locate and convert txt file to string
        NSString *path = [[NSBundle mainBundle] pathForResource:@"country_name" ofType:@"txt"];
        NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

        // seperate string into array
        permanentArray = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]mutableCopy];
        // strip white space string
        permanentArray = [[permanentArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != ''"]]mutableCopy];

        // enumerate a-z to set the two global variables
        for (int asciicode =65; asciicode<=90; asciicode++)
        {
            NSString *initial = [NSString stringWithFormat:@"%c",asciicode];
            self.countryNameInitials = [NSMutableArray arrayWithObject:initial];

            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@",initial];
            NSMutableArray *array = [[permanentArray filteredArrayUsingPredicate:predicate]mutableCopy];

            // populate the dictionary with initial-country-names pair
            [self.countryNameDictionary setObject:array forKey:initial]; // PROBLEM IS HERE

            NSLog(@"self.countrynameDictionary is %@",self.countryNameInitials);
        }

        // set to a non-nil value
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"isFirstLaunch"];
    }

    return YES;
}
Philip007
  • 3,190
  • 7
  • 46
  • 71

1 Answers1

2

Your property definition returns copy of original object when using getter on said property. Since it's a copy, you get immutable object. Try changing copy to strong or retain depending on your used memory management.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • @Philip007 See [this](http://stackoverflow.com/q/816720/730701) to see how to deal with mutable objects as properties. – Adam Sep 13 '12 at 14:26