目的 c でテキスト ファイルから読み取ったデータを使用しようとしています。テキストファイルから読み取ったデータは次のとおりです。
{"aps":{"alert":"Test 1!","sound":"beep.wav","badge":5,"Type":"Banking"},"acme1":"bar","acme2":42}|{"aps":{"alert":"Test 2!","sound":"beep.wav","badge":5,"Type":"Banking"},"acme1":"bar","acme2":42}|{"aps":{"alert":"Test 3!","sound":"beep.wav","badge":5,"Type":"Banking"},"acme1":"bar","acme2":42}|{"aps":{"alert":"Test 4!","sound":"beep.wav","badge":5,"Type":"Banking"},"acme1":"bar","acme2":42}|{"aps":{"alert":"Test 5!","sound":"beep.wav","badge":5,"Type":"Banking"},"acme1":"bar","acme2":42}
読み取ったら、ファイルを区切り文字「|」で配列に分割します。次に、キー「タイプ」に基づいて、銀行、詐欺、投資の 3 つの異なる配列にさらに分けたいと思います。ただし、JSON文字列を配列に分割すると、JSON文字列を解析できないようです。私のビューはロード方法を以下に示しました:
- (void)viewDidLoad {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];
NSArray *fileData = [fileContents componentsSeparatedByString:@"|"];
if (fileContents != NULL)
{
bankingNotifications = [[NSMutableArray alloc] init];
fraudNotifications = [[NSMutableArray alloc] init];
investmentNotifications = [[NSMutableArray alloc] init];
for (i = 0; i < [fileData count]; i++)
{
NSString *notification = fileData[i];
NSDictionary *json = [notification JSONValue];
NSArray *items = [json valueForKeyPath:@"aps"];
if ([[[items objectAtIndex:i] objectForKey:@"Type"] isEqual: @"Banking"])
{
[bankingNotifications addObject:fileData[i]];
NSLog(@"Added object to banking array");
}
if ([[[items objectAtIndex:i] objectForKey:@"Type"] isEqual: @"Fraud"])
{
[fraudNotifications addObject:fileData[i]];
NSLog(@"Added object to fraud array");
}
if ([[[items objectAtIndex:i] objectForKey:@"Type"] isEqual: @"Investment"])
{
[investmentNotifications addObject:fileData[i]];
NSLog(@"Added object to investment array");
}
} }
次の 3 行にエラーがあります。
NSString *notification = fileData[i];
NSDictionary *json = [notification JSONValue];
NSArray *items = [json valueForKeyPath:@"aps"];
JSON 文字列を 3 つの変更可能な配列に解析するのを手伝ってくれませんか? 私が得ているエラーは次のとおりです。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance 0x1d59db30'