Save the data in tableview to NSUserDefaults

综合技术 2018-06-17 阅读原文

Tricks.h file

#import "Tricks.h"

@implementation Tricks

static NSMutableArray *trickList = nil;

+(NSMutableArray *)trickList
        trickList = [[NSMutableArray alloc]init];

    return trickList;


Tricks.m file

@interface Tricks : NSObject

@property(strong, nonatomic) NSString *trickName;

Method for adding objects to array


    Tricks *trick = [[Tricks alloc]init];
    trick.trickName = self.trickLabel.text;
    [[Tricks trickList]insertObject:trick atIndex:0];
    [self.navigationController popViewControllerAnimated:YES];

In .h file of UITabelview class I am making a reference to tricks class, but I am sure there is error on this line.

@property (strong, nonatomic) Tricks *tricks;

In cellForRow method I am storing data

_trick = [[NSMutableDictionary alloc]initWithObjectsAndKeys:trick,nil];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:_trick forKey:@"numberArray"];
[defaults synchronize];

In .m class of UITableview in viewDidLoad I want to retrieve data

if([[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"] != nil) {
        _tricks = [[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"];


Thanks for advices

You are losing all the data because you are trying to save Tricks thats is NSObject
and gets destroyed when your app gets killed, it works while the app is running and you create the NSUserDefaults
better don't save it as Tricks objects, instead you can store your data as NSDictionary
, or NSString
which NSUserDefaults can store, and when you want to use it, you can create Trick
object using your saved NSDictionary

EditOnly NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary can be stored using NSUserDefaults

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


Storing and opening data in a variable I'm making an app that calculates certain things. I need it to be able to take the input from the first textfields, fo...
Distribute the MonoTouch application to testers us... I am trying to archive my MonoTouch app and to supply it to our testers using Apple's/Xcode's organizer. Can somebody ...
iOS自动化打包之重签名导出不同证书ipa探索... 在完成基础的自动包打包流程过后, 随即也出现了日常中常见的问题, 比如我们每次需要打出不同网络环境和不同证书的ipa, 由于开发者可以添加的设备只有100个, 而公司的几个项目都是用的一个账号, 各项目组都是独立的, 再加上期间加入设备的员...
Dart语言之旅:变量 变量 以下是创建变量并为其分配值的示例: var name = 'Bob'; 变量是引用。名为 name 的变量包含对值为“Bob”的String类型的对象的引用。 默认值 未初...
Spring MVC注解、标签库、国际化 本篇文章主要介绍自己在学习Spring MVC常用注解、标签库、国际化遇到的一些问题,分享给大家,希望对你有所帮助。 问题一:指定扫描包的位置 应该将所有控制器类都放在基本包下,并且指定该扫描包,避免Spring MVC扫描了无关的...