In the below code, I want to calculate the net sum of intensity at the end of the loop, but the objective C calls zsum undeclared variable in the NSlog. How do I calculate Sum at the end off loop and pass it outside the loop.
在下面的代码中,我想计算循环结束时的强度净值,但是目标C在NSlog中调用zsum undeclared变量。如何在结束时循环计算Sum并将其传递到循环外部。
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int x = 1; x < 20; x++) {
double zsum = 0;
NSMutableArray *row = [NSMutableArray new];
for (int y = 1; y < 20; y++) {
uchar intensity= curFrame.at<uchar>(cvPoint(y, x));
double zebra = double (intensity)/255;
zsum+= zebra;
[row addObject:[NSNumber numberWithChar:intensity]];
}
// NSLog(@"%d", x);
//NSLog(@"%f",d);
[arr addObject:[NSNumber numberWithInt:zsum]];
// [matrix addObject:row];
}
NSLog(@“%f",zsum);
1 个解决方案
#1
1
So move the decaration of zsum outside of the for loop, after the declaration of arr and just before the for loop:
因此,在声明arr之后和for循环之前,将zsum的decaration移动到for循环之外:
NSMutableArray *arr = [[NSMutableArray alloc] init];
double zsum = 0;
for (int x = 1; x < 20; x++) {
//The rest of your for loop code goes here.
}
In Objective-C, any time code is enclosed in braces ({
and }
) that defines a new scope. Any variables declared inside those braces only exist inside those braces. When you exit the braces, the variables "go out of scope" and no longer exist. Variables defined in an outer scope are visible to more deeply nested levels of scope, but not outside.
在Objective-C中,任何时间代码都包含在用于定义新范围的大括号({和})中。在这些大括号内声明的任何变量只存在于这些大括号内。退出大括号时,变量“超出范围”并且不再存在。外部作用域中定义的变量对于更深层嵌套的作用域级别是可见的,但不在外部。
{
int a;
{
//a is visible
int b;
//a and b are both visible
}
//b is no longer visible
{
//int c
//a and c are visible
{
int d;
//a, c, and d are visible
}
//a and c are visible, d is no longer visible.
}
//only a is visible
}
//None of the vars are visible
The code above is quite contrived, but valid. You would not likely have braces that only define levels of scope, but you could. More likely they would enclose blocks, the bodies of if statments, functions, etc.
上面的代码非常人为,但有效。您可能不会有仅仅定义范围级别的大括号,但您可以。更有可能的是,它们会封闭块,如果是法官,功能等等。
#1
1
So move the decaration of zsum outside of the for loop, after the declaration of arr and just before the for loop:
因此,在声明arr之后和for循环之前,将zsum的decaration移动到for循环之外:
NSMutableArray *arr = [[NSMutableArray alloc] init];
double zsum = 0;
for (int x = 1; x < 20; x++) {
//The rest of your for loop code goes here.
}
In Objective-C, any time code is enclosed in braces ({
and }
) that defines a new scope. Any variables declared inside those braces only exist inside those braces. When you exit the braces, the variables "go out of scope" and no longer exist. Variables defined in an outer scope are visible to more deeply nested levels of scope, but not outside.
在Objective-C中,任何时间代码都包含在用于定义新范围的大括号({和})中。在这些大括号内声明的任何变量只存在于这些大括号内。退出大括号时,变量“超出范围”并且不再存在。外部作用域中定义的变量对于更深层嵌套的作用域级别是可见的,但不在外部。
{
int a;
{
//a is visible
int b;
//a and b are both visible
}
//b is no longer visible
{
//int c
//a and c are visible
{
int d;
//a, c, and d are visible
}
//a and c are visible, d is no longer visible.
}
//only a is visible
}
//None of the vars are visible
The code above is quite contrived, but valid. You would not likely have braces that only define levels of scope, but you could. More likely they would enclose blocks, the bodies of if statments, functions, etc.
上面的代码非常人为,但有效。您可能不会有仅仅定义范围级别的大括号,但您可以。更有可能的是,它们会封闭块,如果是法官,功能等等。