1

我阅读了很多关于 SO 上相同错误消息的帖子,但这些解决方案都不适用于我的情况。所以在推荐其他帖子之前请阅读代码。提前致谢。

我设置了异常断点,发现问题出在[self.countryNameDictionary setObject:array forKey:initial];

在 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

在 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;
}
4

1 回答 1

2

copy在所述属性上使用 getter 时,您的属性定义返回原始对象。由于它是副本,因此您将获得不可变的对象。尝试更改copystrongretain取决于您使用的内存管理。

于 2012-09-13T14:15:49.350 回答