Objective c nsmutable数组目标c中的无序排列objectindex
我想在NSMutableArray中洗牌我的整个ObjectIndex 例如,如果我的JSON应该是这样的:Objective c nsmutable数组目标c中的无序排列objectindex,objective-c,Objective C,我想在NSMutableArray中洗牌我的整个ObjectIndex 例如,如果我的JSON应该是这样的: [{@"name":@"Vikas" , @"Id":@"1",@"subject":@"english"}, {@"Name":@"Rajat",@"Id":@"2",@"Subject":@"Math"}, {@"Name":@"Jhon",@"id":@"3",@"Science"}] [{@"Name":@"Jhon",@"id":@"3",@"Science"}, {@"Na
[{@"name":@"Vikas" , @"Id":@"1",@"subject":@"english"},
{@"Name":@"Rajat",@"Id":@"2",@"Subject":@"Math"},
{@"Name":@"Jhon",@"id":@"3",@"Science"}]
[{@"Name":@"Jhon",@"id":@"3",@"Science"},
{@"Name":@"Rajat",@"Id":@"2",@"Subject":@"Math"},
{@"name":@"Vikas" , @"Id":@"1",@"subject":@"english"}]
我希望结果应该是这样的:
[{@"name":@"Vikas" , @"Id":@"1",@"subject":@"english"},
{@"Name":@"Rajat",@"Id":@"2",@"Subject":@"Math"},
{@"Name":@"Jhon",@"id":@"3",@"Science"}]
[{@"Name":@"Jhon",@"id":@"3",@"Science"},
{@"Name":@"Rajat",@"Id":@"2",@"Subject":@"Math"},
{@"name":@"Vikas" , @"Id":@"1",@"subject":@"english"}]
我想洗牌整个对象,但我无法在Objective-C中这样做
- (void)shuffle
{
NSUInteger count = [self count];
if (count <= 1) return;
for (NSUInteger i = 0; i < count - 1; ++i) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount);
[self exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
}
-(无效)洗牌
{
NSU整数计数=[自计数];
如果(count我们有exchangeObjectAtIndex:withObjectAtIndex:已存在。请充分利用它,并以您希望的方式在顶部写入逻辑。在NSMutableArray上创建类别:
@interface NSMutableArray (Shuffle)
- (void)shuffle;
@end
@implementation NSMutableArray (Shuffle)
- (void)shuffle {
for (NSInteger i = self.count-1; i > 0; i--)
{
[self exchangeObjectAtIndex:i withObjectAtIndex:arc4random_uniform((uint32_t)i+1)];
}
}
@end
洗牌你的可变数组:
NSMutableArray *t = [@[@{@"Name":@"Vikas", @"Id":@"1", @"Subject":@"English"},
@{@"Name":@"Rajat", @"Id":@"2", @"Subject":@"Math"},
@{@"Name":@"John", @"Id":@"3", @"Subject":@"Science"}] mutableCopy];
[t shuffle];
我希望将我的整个objectindex转换为另一个objectindex。请帮助我感谢您的代码具有自计数功能。这意味着您有一个类别或自定义类用于存储json对象的nsmutablearray。显示更多代码,说明您如何在项目中创建数组,以及如何从json加载数组。