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!阅读原文】。感谢您的支持!


Is there a way to share videos with SLComposeViewC... I need to allow users to share video and images to fb and twitter. I recently found the SLComposeViewController but i...
项目总结 7 – 视图样式可配置化 2c8d6bbd2b1204b0dbc0b46008033c36 需求由来 在项目开发过程中,设计师调整设计稿是正常的,但如果调整频率一高,就让我们开发十分抓狂。 我们来进行一个情景模拟(以 AutoLayo...
iOS OS X 和 iOS 中的多线程技术-1 //联系人:石虎QQ:1224614774昵称:嗡嘛呢叭咪哄 一、概念 多线程的目的是,通过并发执行提高 CPU 的使用效率,进而提供程序运行效率。 OS X 和 iOS 是多线程操作系统,它们追随 UNIX 系...
Recording mp3 on iPhone by SDK Play mp3 on iphone sdk? -(IBAction)musiconButtonClicked:(id)sender{ NSString *path = pathForResou...
How to make your HTML5 game look nice on the iPhon... One of the most interesting things you can do once you have built your HTML5 game is to port it on mobile devices as na...