0

我有一个按钮,单击它我将当前引号的索引存储到一个数组中。问题是它覆盖了同一个对象。如何使用不同的对象更改它NSNumber以存储在 中NSMutableArray

[someButton setImage:[UIImage imageNamed:@"add_favoritegold.png"] forState:UIControlStateHighlighted];
    NSUserDefaults *favoriteQuotes = [NSUserDefaults standardUserDefaults];
    NSMutableArray *ListOfIndex= [[NSMutableArray alloc] init];

    //Want to change with diffrent objecy 
    **NSNumber* Wrapped = [NSNumber numberWithInt:index];**

    [ListOfIndex addObject:Wrapped];
    int xOut = [[ListOfIndex lastObject] intValue];
    NSLog(@"%d",xOut);
    [favoriteQuotes setObject:ListOfIndex forKey:@"Indexes"];
4

2 回答 2

0

尝试这个

- (void)saveFavouriteQuoteIndex:(NSUInteger)index
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *savedQuoteIndexes = [[defaults objectForKey:@"Indexes"]mutableCopy];

    if (!savedQuoteIndexes) {
        savedQuoteIndexes = [NSMutableArray array];
    }

    NSNumber *indexNumber = @(index);

    if (![savedQuoteIndexes containsObject:indexNumber]) {
        [savedQuoteIndexes addObject:indexNumber];

        [defaults setObject:savedQuoteIndexes forKey:@"Indexes"];
        [defaults synchronize];
    }

}
于 2013-04-11T10:06:37.410 回答
0

您正在ListOfIndex使用密钥保存为Indexes.

因此,对于下一个值,它将替换具有 key 的先前值Indexes

编辑:

读取数组

 NSArray *readArray=favoriteQuotes[@"Indexes"];

将新值添加到同一个数组中。

于 2013-04-11T09:56:13.287 回答