Frage

Ich versuche etwas relativ Einfaches zu tun.Ich habe eine .plist in meinem Paket und versuche, sie verschlüsselt im Dokumentenverzeichnis zu speichern.Bevor ich versucht habe, die Verschlüsselung hinzuzufügen, hat es einwandfrei funktioniert.Es kam jedoch zu einem neuen Absturz.

So speichere ich meine .plist:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]) {
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"GameSave" ofType:@"plist"];
    NSData *plistData = [NSData dataWithContentsOfFile:bundle];
    [NSKeyedArchiver archiveRootObject:plistData toFile:path];
    plistData = [plistData AES256EncryptWithKey:@"536335"];
    [plistData writeToFile:path atomically:YES];
}

Dann rufe ich auf diese Weise meine .plist ab (und ändere später einen Wert und speichere erneut).

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];

        //Get array and then current level dict
        NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
        plistData = [plistData AES256DecryptWithKey:@"1111"];

        NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy];
        int objectIndex = [Singleton sharedInstance].levelNumber - 1;
        NSMutableDictionary *levelDict = [[savegameArray objectAtIndex:objectIndex] mutableCopy];
        [levelDict setObject:videoID forKey:@"RecordingURL"];
        //Now put new dict back in array
        [savegameArray replaceObjectAtIndex:objectIndex withObject:levelDict];

        NSData *savedarrayData = [NSKeyedArchiver archivedDataWithRootObject:savegameArray];
        savedarrayData = [savedarrayData AES256EncryptWithKey:@"1111"];
        [savedarrayData writeToFile:path atomically:YES];

Allerdings steht im Lesecode jedes Mal, wenn ich zu dieser Zeile komme: NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy]; Es gibt einen SIGABRT-Absturz, der Folgendes ausgibt:

'-[__NSCFArray objectForKey:]:Nicht anerkannter Selektor, der an Instanz 0x16604140 gesendet wurde

Was mache ich falsch?

War es hilfreich?

Lösung

Wie wir durch unseren Chat erfahren haben, mussten wir das Entschlüsselte zunächst konvertieren NSData Einwand gegen ein Recht NSPropertyListSerialization Serialisierung.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];

NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
NSData *newData = [plistData AES256DecryptWithKey:@"1111"];

NSPropertyListFormat format;

NSMutableArray *savegameArray = [[NSPropertyListSerialization propertyListFromData:newData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:nil]mutableCopy];

NSLog(@"Array: %@",savegameArray);

Andere Tipps

Sie müssen Ihrem NSMutableArray nur Speicher zuweisen, z.NSMutableArray *arrayName = [[NSMutableArray alloc] init];Bis Sie dies tun, wird es zu einem Absturz kommen.

Ich hoffe, das ist hilfreich.

Danke, Rajesh ..

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top