0

I know this has been asked a million times, but I've been making a mess...

So basically what I'm trying to do is send an object from a details view to a favorites view with all of it's properties.

I've already got a button working to add a string [one property of the object] to favorites

button pressed code:

- (void)buttonPressed:(id) sender
{
    NSMutableArray *favss = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Favorite"]mutableCopy];
    if(favss != nil)
    {
        NSLog(@"Array found. Contents: %@",favss);
    }
    else
    {
    favss = [[NSMutableArray alloc] initWithCapacity:0];
    }
    [favss addObject:self.word.head];
    [[NSUserDefaults standardUserDefaults] setObject:favss forKey:@"Favorite"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"Number of items in my array is: %d", [favss count]);
}

my object is stored in it's own class:

@interface Words : NSObject {
NSString *head;
NSString *pro;
NSString *def;
}

@property(nonatomic, copy) NSString *head;
@property(nonatomic, copy) NSString *pro;
@property(nonatomic, copy) NSString *def;

when trying to run [favss addObject:self.word] the app obviously freaked out because objects can't be added to NSUserDefaults -- so I tried NSCoding but that turned my entire details view BLACK -- I've tried sending my three strings separately and that's fine but then they are all completely separated which kind of defeats the purpose -- and I somehow end up completely jacking up my arrays with all the different code I've been trying because now I keep getting this error:

Array found. Contents: <62706c69 73743030 d4010203 0405081d 1e542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a6090a13 14151655 246e756c 6cd40b0c 0d0e0f10 11125468 65616456 24636c61 73735370 726f5364 65668002 80058003 8004615d f4536261 316f10cd 005b0070 00612075 2075005d 00202460 7c98003b 00208d34 002025b7 59277ea2 559c62a5 ff5e4e0a 58993002 24615207 54080020 25b77b54 662f7b54 5f978d77 ff0c5c31 60154e0d ff5e9898 30022462 63287740 002025b7 524d4e0d ff5e6751 ff0c540e 4e0dff5e 5e973002 24639644 7740003b 00208ddf 968f0020 25b7674e 5f3a83ab 5f977968 ff0cff5e 50127535 5f719662 76848001 738b5165 4e86573a 30022464 62767740 002025b7 624bff5e 95e8678b 5f805916 770b3002 24656cbf 7740003b 0020987a 77400020 25b74f60 ff5e6cb3 8fb95f80 4e0b8d70 ff0c4e0d 8fdc5c31 662f4e5d 773c6865 30022466 4eb28fd1 002025b7 4f607231 5a03513f ff0c5a03 513f5c31 ff5e4f60 30022467 72758fde 002025b7 90a35e74 5b506211 72385173 8fdb0020 201c725b 68da201d ff0c5f20 53d453d4 89c15230 62118fde 740690fd 4e0d7406 ff0c751f 6015628a 4ed6ff5e 50124e86 30022468 5df47ed3 002025b7 4f604ee5 4e3a628a 79d1957f ff5e5012 5c316709 597d5904 55e6003f 00202469 7167987e 003a0020 89c10020 201c5df4 5bb6201d 3002d217 18191c58 24636c61 73736573 5a24636c 6173736e 616d65a2 1a1b5557 6f726473 584e534f 626a6563 7455576f 72647312 000186a0 5f100f4e 534b6579 65644172 63686976 65720008 00110016 001f0028 00320035 003a003c 00430049 00520057 005e0062 00660068 006a006c 006e0071 00750212 02170220 022b022e 0234023d 02430248 00000000 00000201 00000000 0000001f 00000000 00000000 00000000 0000025a>

Basically, I just want my button to add the object that is being displayed in the details view into an array that is passed into NSUserDefaults to be displayed in a table view on the other side (which can then be selected to display the details of each object again)...and I know it's been asked a lot but...well, I can't even display my array at the moment...

Mou某
  • 556
  • 3
  • 10
  • 32
  • I'm having a little trouble getting exactly what your question is... You finish your questions with talking about adding a value to an array in NSUserDefaults, but you also talk about a custom object (Word). You can't store a custom object there. So what is it exactly that you need to store? – Marius Waldal Mar 11 '14 at 10:40
  • @MariusFalkenbergWaldal I want to store my custom object into an array in nsuserdefaults. My custom objects are already loaded into a table and detail view. – Mou某 Mar 11 '14 at 12:36

2 Answers2

1

If I understand you correctly, you need to store an array of objects (Word) that has 3 values (head, pro, def).

I suggest you store those three values in an NSDictionary: head="xxx" pro="yyy" def="zzz"

and then you store this NSDictionary in an array, which is then stored in NSUserDefaults.

If you are adamant in storing your custom objects, you need to expand them a little.

The answer to this SO questions should get you there: How to store custom objects in NSUserDefaults

Community
  • 1
  • 1
Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • thanks for the link, but like I've said I've tried a lot of things and nothing has really worked for me...so I was hoping for something a little more specific – Mou某 Mar 14 '14 at 07:29
  • I think that was pretty specific? You need to implement the NSCoding protocol, and the linked SO post have example code for that. All you need to do is replace the encodeObject and decodeObjectForKey with your specific properties (head, pro, def). Did you try using NSCoding? – Marius Waldal Mar 14 '14 at 07:42
  • When? If you implement the encodeWithCoder and initWithCoder in your Word class? Did you try debugging to find out what happens, or how far the code executes? – Marius Waldal Mar 14 '14 at 07:54
  • okay fixed that but still getting same problem i wrote in my question `Array found. Contents: <62706c69 73743030 d4010203 0405080a 0b542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748000 a1095524 6e756c6c 12000186 a05f100f 4e534b65 79656441 72636869 76657208 11161f28 32353a3c 3e444900 00000000 00010100 00000000 00000c00 00000000 00000000 00000000 00005b>` – Mou某 Mar 14 '14 at 08:19
  • Are you trying to use NSUserDefaults as a temporary storage for an object you want to transfer from one view to another? – Marius Waldal Mar 14 '14 at 08:26
  • yeah i'm trying to add a word to an array that will serve as "favorites" – Mou某 Mar 14 '14 at 08:47
  • I'm not so sure this is a good scenario for NSUserDefaults. You state in your initial question that you want to send an object from details view to favorites view. How do you store the user's favorites between sessions? How does your app persist the user's favorites? – Marius Waldal Mar 14 '14 at 09:22
  • I have an array on the other side and it passes it from the one view to the other view - when I used a string it passed fine and saved fine... – Mou某 Mar 14 '14 at 10:23
  • That was not what I asked. One thing is when you move from details to favorites, but what if you exit the app and open it later? And what is "the other side"? – Marius Waldal Mar 14 '14 at 10:25
  • favorites has it's own array. I have button on my details page that, when clicked, will send the object that is currently being displayed to favorites - the view does not change. details view takes in the info puts it into an array which then goes into nsuserdefaults at which point the array in favorites is then populated with the array from details. – Mou某 Mar 14 '14 at 10:37
  • So, does the favorites view always read from user defaults, every time the app loads? – Marius Waldal Mar 14 '14 at 12:57
  • Then you should redesign your app to pass these values in another way. You're misusing the NSUserDefaults as a temporary storage to alleviate a design flaw. Instead of spending more time working around the flaw you should solve it in another way. How do you go from detail view to favorites view? How does the favorites view know about your favorites the next time you open the app? If access to the favorites view is always (or mostly) via the details view, you should pass the values during a segue or similar. – Marius Waldal Mar 14 '14 at 15:43
-1

i did it like this in the end:

- (void)apopbuttonPressed:(id) sender
{

    NSMutableArray *myfavs = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MyFavoritez"]mutableCopy];
    if(myfavs != nil)
    {
        NSLog(@"Array found. Contents: %@",myfavs);
    }
    else
    {
        myfavs = [[NSMutableArray alloc] initWithCapacity:0];
    }
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"MyFavoritez"];
    if (dataRepresentingSavedArray != nil)
    {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if (oldSavedArray != nil)
            _myfavs = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            _myfavs = [[NSMutableArray alloc] init];
    }

    [_myfavs addObject:self.word];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_myfavs] forKey:@"MyFavoritez"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"Number of items in my array is: %d", [_myfavs count]);
   // [self performSegueWithIdentifier:@"passFavs" sender:favs];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([[segue identifier] isEqualToString:@"passFavs"]) {
        FavViewController *con = segue.destinationViewController;
        con.myFavsTwo = _myfavs;    }
}

and

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    myFavsTwo = [NSKeyedUnarchiver unarchiveObjectWithData:[[[NSUserDefaults standardUserDefaults] objectForKey:@"MyFavoritez"] mutableCopy]];
    [self.tableView reloadData];
    NSLog(@"Number of items in my array is: %d", [myFavsTwo count]);
}

in case anyone else runs into similar issues

Mou某
  • 556
  • 3
  • 10
  • 32
  • I don't understand this. In prepareForSegue, you pass _myfavs to the FavViewController, but in FavViewController, you don't use this? In the viewWillAppear method you get the favs from nsuserdefaults, and disregard what you passed in from the segue. Also, when I asked you if the favorites controller will always read from user defaults every time it opens (which you now do), you answered that it is always "passed along". I can see no sensical structure in the solution you've created. – Marius Waldal Mar 17 '14 at 07:21