Search code examples
objective-cnsuserdefaultsnskeyedarchiver

-[__NSCFArray bytes]: unrecognized selector sent to instance


I have saved the array of my products in the user default as below:

  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  [userDefault setObject:[NSKeyedArchiver archivedDataWithRootObject:plans] forKey:@"plans"];
  [[NSUserDefaults standardUserDefaults] synchronize];

and read the default as below:

    NSArray *plans;
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"plans"];
    if (data != nil)
    {
        plans = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }

But sometimes my app getting crashed with -[__NSCFArray bytes]: unrecognized selector sent to instance while calling unarchiveObjectWithData method.


Solution

  • At some point you must have stored an NSArray into NSUserDefaults instead of archived data. Have you tried clearing your user defaults and trying again?