How to save NSMutablearray in NSUserDefaults

前端 未结 9 1131
盖世英雄少女心
盖世英雄少女心 2020-11-29 17:50

I have two NSMutableArray\'s. They consist of images or text. The arrays are displayed via a UITableView. When I kill the app the data within the <

相关标签:
9条回答
  • 2020-11-29 18:13

    Swift Version:

    Save Array in NSUserDefaults:

    NSUserDefaults.standardUserDefaults().setObject(selection, forKey: "genderFiltersSelection")
    

    Retrieve Bool Array From NSUserDefaults:

    if NSUserDefaults.standardUserDefaults().objectForKey("genderFiltersSelection") != nil{
                    selection = NSUserDefaults.standardUserDefaults().objectForKey("genderFiltersSelection") as? [Bool] ?? [Bool]()
       }
    

    Retrieve String Array From NSUserDefaults:

    if NSUserDefaults.standardUserDefaults().objectForKey("genderFiltersSelection") != nil{
                    selection = NSUserDefaults.standardUserDefaults().objectForKey("genderFiltersSelection") as? [String] ?? [String]()
        }
    
    0 讨论(0)
  • 2020-11-29 18:17

    If you need to add strings to the NSMutableArray in ant specific order, or if you are using the NSMutableArray for a UITableView you may want to use this instead:

    [NSMutableArray insertObject:string atIndex:0];
    
    0 讨论(0)
  • 2020-11-29 18:17

    Save information of Array in NSUserdefaults with key.

    "MutableArray received from JSON response"

    [[NSUserDefaults standardUserDefaults] setObject:statuses forKey:@"arrayListing"];
    

    "Retrieve this information(Array) anywhere in the project with same key"

    NSArray *arrayList = [[NSUserDefaults standardUserDefaults] valueForKey:@"arrayListing"];
    

    This helped me in my project and hope, it will help someone.

    0 讨论(0)
提交回复
热议问题