UA-17470720-3

Jump to content


Photo
- - - - -

NSUserDefaults binding to MutableArray

data-stores general

  • Please log in to reply
2 replies to this topic

#1 opcheese

opcheese

    Starting Out

  • STV 5.0 Std
  • Pip
  • 4 posts
Reputation: 1
Good

Posted 21 June 2013 - 01:38 AM

Hello!
 
I create settings view for my app using NSUserDefaultsBinding.
 
I store an array in userdefaults.

  • How can provide user with option to reorder such array?
  • How can I set a localized titles for array's items?
SCPropertyDefinition *languageDef = [userDefaultsDef propertyDefinitionWithName:@"excursionlanguages"];
languageDef.title = NSLocalizedString(@"Language", nil);
languageDef.type = SCPropertyTypeSelection;
SCObjectSelectionAttributes * atr = [SCObjectSelectionAttributes attributesWithItems:@[@"Ru",@"En",@"Fr"] allowMultipleSelection:NO allowNoSelection:YES];
atr.allowMovingItems = YES;
languageDef.attributes = atr;
 

 

 

Thanx,
Arseny



#2 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 21 June 2013 - 12:32 PM

Hi Arseny,

 

Good question.  

 

In regards to re-ordering the list.  First, you must be using a NSMutableArray for the "moving" features (re-order).  If I recall correctly, NSUserDefaults only returns immutable objects - meaning that by default, you cannot mutate or change them.  So, if  you must keep your "list" in user defaults, then you need to pull it from there, copy it to a mutable equivalent then save it back.  

 

I'm not sure if you are trying to work on your list as a "selection list" or as an ArrayOfObjects.  

 

Here is a quick snippet showing how to manage storing a "list" in user defaults, getting, changing it and saving it back. 

 

 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    if ([prefs objectForKey:@"orderedList"] == nil) {

        NSMutableArray *ma = [@[@"item1",@"item2",@"item3",@"item4",@"item5"] mutableCopy];

        [prefs setObject:ma forKey:@"orderedList"];

        [prefs synchronize];

    }

    

    NSMutableArray *reorderedList = [prefs objectForKey:@"orderedList"];

    

    NSMutableDictionary *md = [NSMutableDictionary dictionaryWithObject:reorderedList forKey:@"orderedList"];

    SCDictionaryDefinition *dictDef = [SCDictionaryDefinition definitionWithDictionaryKeyNamesString:@"orderedList"];

    

    SCPropertyDefinition *item = [dictDef propertyDefinitionWithName:@"orderedList"];

    item.type = SCPropertyTypeArrayOfObjects;

    item.attributes = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:[SCStringDefinition definition] allowAddingItems:YES allowDeletingItems:YES allowMovingItems:YES expandContentInCurrentView:NO placeholderuiElement:nil addNewObjectuiElement:[SCTableViewCell cellWithText:@"new object"] addNewObjectuiElementExistsInNormalMode:YES addNewObjectuiElementExistsInEditingMode:YES];

    item.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)

    {

        if ([cell.boundValue isKindOfClass:[NSMutableArray class]]) {

            NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

            [def setObject:cell.boundValue forKey:cell.boundPropertyName];

            [def synchronize];

        }

    };

    

    [self.tableViewModel generateSectionsForObject:md withDefinition:dictDef];

 

 

If you want to capture a selection choice from a list that is localized, you would have to modify your detail view either on the fly using a willDisplay block to localize what is displayed in the cell's text label.

 

hope that helps.


  • opcheese likes this

#3 opcheese

opcheese

    Starting Out

  • STV 5.0 Std
  • Pip
  • 4 posts
Reputation: 1
Good

Posted 02 July 2013 - 02:42 AM

Thanx!







Also tagged with one or more of these keywords: data-stores, general

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users