0

我在我的创建两个NSMutableArrayviewDidLoad我将它添加到一个NSMutableDictionary。当我尝试用一​​个数组对其进行改组时,没关系。但问题是当我独立地洗牌两个数组时它不起作用,不知何故索引混淆了。

这是我的数组(第一个数组)的代码:

self.items1 = [NSMutableArray new];

for(int i = 0; i <= 100; i++) 
{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"[Images%d.png", i]]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        self.container = [[NSMutableDictionary alloc] init];
        [container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"items1"]; 
        [container setObject:[NSNumber numberWithInt:i] forKey:@"index1"];
        [items1 addObject:container];           
    } 
} 

NSLog(@"Count : %d", [items1 count]);  

[items1 enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
    NSLog(@"%@ images at index %d", object, index);
}];

(第二个阵列):

self.items2 = [NSMutableArray new];  
for(int i = 0; i <= 100; i++) 
{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"secondImages%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        self.container = [[NSMutableDictionary alloc] init];
        [container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"items2"]; 
        [container setObject:[NSNumber numberWithInt:i] forKey:@"index2"];
        [items2 addObject:container];
    } 
} 

NSLog(@"Count : %d", [items2 count]);  

[items2 enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
    NSLog(@"%@ images at index %d", object, index);
}];

我对iCarousel使用我的数组的地方的看法:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

    if (carousel == carousel1)
    {
        NSDictionary *obj = [items1 objectAtIndex:index];
        view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"items1"]];
        view.tag = index;
    }
    else
    {
        NSDictionary *obj = [items2 objectAtIndex:index];
        view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"items2"]];
        view.tag = index;
    }

    return view;
}

然后是我的洗牌代码(我尝试为另一个数组复制,但也不起作用):

   srandom(time(NULL));
    NSUInteger count = [items1 count];
    for (NSUInteger i = 0; i < count; ++i) {
        int nElements = count - i;
        int n = (random() % nElements) + i;
        [items1 exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

我将如何使用上面的代码(或者如果您有其他建议)对两个数组进行洗牌?谢谢

我的另一个问题是,当我尝试为 shuffle 方法子类化该类或使用上面的代码时,它们的索引是混合的。例如:

对象:苹果、球、胡萝卜、狗

索引:1 2 3 4

但在我看来,洗牌时:

对象: 胡萝卜、苹果、狗、芭蕾

索引:2 4 1 3

我还有一个方法,当轮播停止时,它会删除视图中的图像。

4

3 回答 3

1

最干净的解决方案:使用Objective-C 类别

NSMutableArray+RandomUtils.h

#import <Foundation/Foundation.h>


@interface NSMutableArray (RandomUtils) 
-(void)shuffle;
@end

NSMutableArray+RandomUtils.m

#import "NSMutableArray+RandomUtils.h"

@implementation NSMutableArray (RandomUtils)
-(void)shuffle
{
    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        NSUInteger nElements = count - i;
        NSUInteger n = (arc4random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end

导入它,无论你在哪里洗牌一个 MSMutableArray

[array1 shuffle];
[array2 shuffle];

我试图重现您的混合索引问题,但我不能

NSMutableArray *sarray = [NSMutableArray arrayWithArray: @[@"carrots", @"apple", @"dog", @"balle"]];

NSLog(@"%@", sarray);
[sa enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@ object at index %lu", obj, idx);
}];

[sarray shuffle];
 NSLog(@"%@", sarray);
[sarray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@ object at index %lu", obj, idx);
}];

结果是

(
    carrots,
    apple,
    dog,
    balle
)
carrots object at index 0
apple object at index 1
dog object at index 2
balle object at index 3
 (
    balle,
    dog,
    carrots,
    apple
)
balle object at index 0
dog object at index 1
carrots object at index 2
apple object at index 3

顺便说一句:您的示例也不正确:数组中有四个对象,索引应以 0 开头,最大值为 3。您有 1 和 4。

所以数组被洗牌并且索引没问题。您在代码中一定有其他问题,您没有显示。

你应该放弃你的真实代码。


我仍然不确定,如果我理解,你想要什么。但是根据您的评论,我假设您想对数组进行洗牌但保留旧索引。这是不可能的,因为索引是数组中对象的位置。但是您总是可以自由地将原始索引保存在某处。
或者你只是复制一个数组,随机播放其中一个。现在 ho 可以请求两个数组中对象的索引。

NSArray *originalArray =  @[@"carrots", @"apple", @"dog", @"balle"];

NSLog(@"%@", originalArray);
[originalArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@ object at index %lu", obj, idx);
}];


NSArray *shuffledArray = [originalArray arrayShuffled];
 NSLog(@"%@", shuffledArray);
[shuffledArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@ object at index %lu %lu", obj, idx, [originalArray indexOfObject:obj]);
}];
于 2012-10-16T11:36:02.460 回答
0

假设这个想法是以相同的顺序洗牌两个数组,那么你只需要在第一个数组之后交换第二个数组吗?

NSUInteger count = [items1 count];
for (NSUInteger i = 0; i < count; ++i) {
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [items1 exchangeObjectAtIndex:i withObjectAtIndex:n];
    [items2 exchangeObjectAtIndex:i withObjectAtIndex:n];
}
于 2012-10-16T04:11:41.047 回答
0

假设您有两个不同的图像数组,您需要对图像进行洗牌。

 for(int i = 0; i <= 100; i++) 
    {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];

        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"[Images%d.png", i]]; 
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
//Lazy initialization of dictionary 
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"items1"]; 
            [dict setObject:[NSNumber numberWithInt:i] forKey:@"index1"];
            [items1 addObject:dict]; 
    [dict release];          
        } 
    }

以相同的方式将对象添加到第二个数组。

NSUInteger count = [items1 count];
for(int i = 0 ;i < count; i++)
{
int nElements = count - i;
int n = (random() % nElements) + i;
[items1 exchangeObjectAtIndex:i withObjectAtIndex:n];
} 

第二个数组洗牌

 NSUInteger count = [items2 count];
    for(int i = 0 ;i < count; i++)
    {
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [items2 exchangeObjectAtIndex:i withObjectAtIndex:n];
    } 

这应该有效。如果您仍然发现问题,请告诉我。

于 2012-10-16T06:22:36.850 回答