search

Home  >  Q&A  >  body text

ios - NSUserDefaults存取long变量时有问题

int userid = userId;
[userDefault setInteger:userid forKey:@"userId"];

long userId = [[NSUserDefaults standardUserDefaults] integerForKey:@"userId"];

存的时候userId是id类型。
这时候我取出的数是一个很大的数。请问这是怎么回事?

怪我咯怪我咯2771 days ago612

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:46:07

    Use setObject to save the id variable when saving, and use integerForKey when fetching.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 15:46:07

    The problem is

    int userid = userId;

    should be changed to

    NSInteger userid = userId;
    ...

    Because
    NSInteger != int

    See picture

    reply
    0
  • Cancelreply