我在NSMutableArray
中创建了两个viewDidLoad
,我将其添加到NSMutableDictionary
中。当我试着用一个阵列洗牌时,没关系。但问题是,当Im独立地移动两个阵列时,它无法正常工作,不知何故索引混淆了。
这是我的数组代码(第一个数组):
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;
}
然后我的shuffle代码(我尝试复制其他数组,但也没有工作):
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
但在我的视图中洗牌:
对象:胡萝卜,苹果,狗,balle
索引:2 4 1 3
我还有一个方法,当旋转木马停止时,它会删除视图上的图像。
答案 0 :(得分:1)
最干净的解决方案:使用Objective-C Category
的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
BTW:你的例子是不正确的:在数组中有四个对象,索引应该从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]);
}];
答案 1 :(得分: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];
}
答案 2 :(得分: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];
}
这应该有效。如果您仍然发现问题请告诉我。