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