Xcode objective-c中的多维数组迭代
我想学习如何使用2DXcode objective-c中的多维数组迭代,xcode,multidimensional-array,iteration,Xcode,Multidimensional Array,Iteration,我想学习如何使用2DNSMutableArray中的每个语句。我的代码如下。它在for语句的第三个(最内层)抛出异常。例外情况是: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance" 我的代码: NSMutableArray* subTryingSet=[NSM
NSMutableArray
中的每个语句。我的代码如下。它在for语句的第三个(最内层)抛出异常。例外情况是:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance"
我的代码:
NSMutableArray* subTryingSet=[NSMutableArray arrayWithArray:[self genSetNumbers:arrRandoms withSize:4]];
for (NSMutableArray* oneRow in subTryingSet) {
for (NSMutableArray* w in oneRow) {
for (int i=0;i<w.count;i++) {
NSLog(@"%d", [[w objectAtIndex:i] intValue]);
}
}
}
NSMutableArray*subTryingSet=[NSMutableArray arrayWithArray:[self GensetNumber:arrRandoms withSize:4]];
对于(NSMutableArray*oneRow in subTryingSet){
用于(oneRow中的NSMutableArray*w){
对于(int i=0;i在第一次快速查看代码后:
尝试更改此选项:
NSLog(@"%d", [[w objectAtIndex:i] intValue]);
与:
编辑
它在第三个“for语句”处抛出异常,因此无法进入slog
嗯…你确定oneRow中的所有对象都是NSMutableArray吗
尝试这样检查:
for (NSMutableArray* oneRow in subTryingSet) {
if ([oneRow.class isSubclassOfClass:[NSMutableArray class]]) {
for (NSMutableArray* w in oneRow) {
if ([w.class isSubclassOfClass:[NSMutableArray class]]) {
for (int i=0;i<w.count;i++) {
NSLog(@"%d", [[w objectAtIndex:i] intValue]);
}
}
}
}
}
for(NSMutableArray*oneRow in subTryingSet){
if([oneRow.class isSubclassOfClass:[NSMutableArray类]]){
用于(oneRow中的NSMutableArray*w){
if([w.class isSubclassOfClass:[NSMutableArray类]]){
对于(int i=0;i在第一次快速查看代码后:
尝试更改此选项:
NSLog(@"%d", [[w objectAtIndex:i] intValue]);
与:
编辑
它在第三个“for语句”处抛出异常,因此无法进入slog
嗯…你确定oneRow中的所有对象都是NSMutableArray吗
尝试这样检查:
for (NSMutableArray* oneRow in subTryingSet) {
if ([oneRow.class isSubclassOfClass:[NSMutableArray class]]) {
for (NSMutableArray* w in oneRow) {
if ([w.class isSubclassOfClass:[NSMutableArray class]]) {
for (int i=0;i<w.count;i++) {
NSLog(@"%d", [[w objectAtIndex:i] intValue]);
}
}
}
}
}
for(NSMutableArray*oneRow in subTryingSet){
if([oneRow.class isSubclassOfClass:[NSMutableArray类]]){
用于(oneRow中的NSMutableArray*w){
if([w.class isSubclassOfClass:[NSMutableArray类]]){
对于(inti=0;i您可以使用这个定制的objective c方法进行迭代
-(void)loopMultArray:(NSArray*)a walk:(void(^)(id node,int index,int zindex))n{
void(^callback)(id node,int index,int zindex) = Block_copy(n);
NSMutableArray *l=[[[NSMutableArray alloc] initWithObjects:a,nil] autorelease];
int c=1;
//This first loop will loop until the count var is stable//
for(int r=0;r<c;r++){
//This loop will loop thru the child element list//
for(int z=0;z<[[l objectAtIndex:r] count];z++){
callback([[l objectAtIndex:r] objectAtIndex:z],z,r);
if([[[l objectAtIndex:r] objectAtIndex:z] isKindOfClass:[NSArray class]]){
[l addObject:[[l objectAtIndex:r] objectAtIndex:z]];
c++;
}//IF
}//FOR
}//FOR
Block_release(callback);
}
loopMultArray:(NSArray*)漫游:(void(^)(id节点,int索引,int zindex))n{
void(^callback)(id节点,int索引,int zindex)=块拷贝(n);
NSMutableArray*l=[[NSMutableArray alloc]initWithObjects:a,nil]autorelease];
int c=1;
//第一个循环将循环,直到count变量稳定//
对于(int r=0;r,您可以使用这个定制的objective c方法进行迭代
-(void)loopMultArray:(NSArray*)a walk:(void(^)(id node,int index,int zindex))n{
void(^callback)(id node,int index,int zindex) = Block_copy(n);
NSMutableArray *l=[[[NSMutableArray alloc] initWithObjects:a,nil] autorelease];
int c=1;
//This first loop will loop until the count var is stable//
for(int r=0;r<c;r++){
//This loop will loop thru the child element list//
for(int z=0;z<[[l objectAtIndex:r] count];z++){
callback([[l objectAtIndex:r] objectAtIndex:z],z,r);
if([[[l objectAtIndex:r] objectAtIndex:z] isKindOfClass:[NSArray class]]){
[l addObject:[[l objectAtIndex:r] objectAtIndex:z]];
c++;
}//IF
}//FOR
}//FOR
Block_release(callback);
}
loopMultArray:(NSArray*)漫游:(void(^)(id节点,int索引,int zindex))n{
void(^callback)(id节点,int索引,int zindex)=块拷贝(n);
NSMutableArray*l=[[NSMutableArray alloc]initWithObjects:a,nil]autorelease];
int c=1;
//第一个循环将循环,直到count变量稳定//
for(int r=0;rIt在第三个“for语句”处抛出异常,因此无法转到nslog。好的,您是对的meronix。这里,w不是数组。我将看看genSetNumbers方法为什么返回“subTryingSet”作为一维数组。非常感谢。我可能会再次问一些有关这种情况的问题。:)实际上,第二个for必须是“for”(oneRow中的NSNumber*w)”。这对我来说是一个困惑。它在第三个“for statement”处抛出异常,因此无法转到nslog。好的,你是对的meronix。这里,w不是数组。我将看看genSetNumbers的方法为什么会将“subTryingSet”返回为一维数组。非常感谢。我可能会再次问一些关于这种情况的问题。:)事实上,第二个for必须是“for(oneRow中的NSW编号)”。这让我感到困惑。