Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“无止境”;至于;iPhone应用程序发布配置中的循环_Iphone_Objective C - Fatal编程技术网

“无止境”;至于;iPhone应用程序发布配置中的循环

“无止境”;至于;iPhone应用程序发布配置中的循环,iphone,objective-c,Iphone,Objective C,我的应用程序中有以下代码: NSArray *lstAttributes = [conditionAttribute componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator], *lstValues = [conditionValue componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSepa

我的应用程序中有以下代码:

NSArray *lstAttributes = [conditionAttribute componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
    *lstValues = [conditionValue componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
    *lstValueTypes = [conditionValueType componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator];

if([lstAttributes count] != [lstValues count] ||
   [lstAttributes count] != [lstValueTypes count]) return NO;

BOOL bResult = YES;
NSLog(@"attributes amount - %u", [lstAttributes count]);
for(uint i = 0; i < [lstAttributes count]; i ++)
{
    NSLog(@"counter: %u", i);
    SystemUIConfigurationAttributeCondition *condition = [SystemUIConfigurationAttributeCondition new];

    condition.conditionAttribute = [lstAttributes objectAtIndex:i];
    condition.conditionValue = [lstValues objectAtIndex:i];
    condition.conditionValueType = [lstValueTypes objectAtIndex:i];

    bResult &= [self checkCondition:condition forOwner:owner];

    FreeObject(&condition);

    if(!bResult) break;
}

return bResult;
NSArray*lstAttributes=[conditionAttribute组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator],
*lstValues=[conditionValue组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator],
*lstValueTypes=[conditionValueType组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator];
如果([lstAttributes count]!=[lstValues count]||
[lstAttributes计数]!=[lstValueTypes计数])返回编号;
BOOL-bResult=是;
NSLog(@“属性数量-%u”,[lstAttributes计数]);
对于(uint i=0;i<[lsttributes count];i++)
{
NSLog(@“计数器:%u”,i);
SystemUIConfigurationAttributeCondition*条件=[SystemUIConfigurationAttributeCondition新建];
condition.conditionAttribute=[lsttributes objectAtIndex:i];
condition.conditionValue=[lstValues objectAtIndex:i];
condition.conditionValueType=[lstValueTypes对象索引:i];
b结果&=[自检条件:所有者条件:所有者];
自由对象(&条件);
如果(!bResult)中断;
}
返回结果;
在“调试”配置中一切正常。但一旦我把它切换到“释放”,我就面临着无尽的循环。控制台向我显示以下属性:数量-2,计数器:0,计数器:1,计数器:1,计数器:1,计数器:1,计数器:1,计数器:1。。。。。。等等

我尝试使用不同的“for”和“while”操作符,但这些都不能正常工作。循环仍然无法停止


以前有人遇到过同样的问题吗?

因为您在评论中提到,如果您注释掉&=操作,它会起作用,所以我将该行更改为:

bResult = bResult && [self checkCondition:condition forOwner:owner];
这是一个布尔AND,这是您在这一行中真正想要做的执行按位AND,这并不总是等同于布尔AND,因为任何非零值的计算结果都为true。例如:

BOOL a = 1;
BOOL b = 2;

if (a && b) {
    NSLog (@"a && b is true"); // This line will execute
}
if (a & b) {
    NSLog (@"a & b is true");  // This line won't execute
}
因为在我们的条件中使用对象地址是很常见的,所以使用按位AND代替布尔AND可能会产生错误。例如,如果您从
getCondition:fromOwner:
返回一个对象地址,希望它的意思是“是”,并且该内存地址恰好是一个偶数,那么带“是”的按位and将计算为“否”,其中布尔and将计算为“是”


我对您的特定错误的原因的最好猜测是,按位AND以某种方式导致了缓冲区溢出,这会踩到您的i变量。如果这个假设是正确的,那么切换到布尔值,也应该解决这个问题。

按照承诺解决此错误:

起初,我尝试使用代码块来枚举列表中的所有对象,而不是“for”循环

但我在第二次迭代中遇到了一个崩溃。“lstValues”和“lstValueTypes”指针突然更改了它们的值,应用程序收到了EXC_BAD_访问。可能使用3个数组而只枚举其中的1个不是一个好主意。调试器显示枚举是在同一线程上执行的,但在第二次迭代时,3个数组中的2个已损坏

因此,我决定将初始循环分为两部分:

  • 准备一份条件清单
  • 检查每个条件
  • 第一个是一个常见的“for”循环,第二个是NSArray类的“enumerateObjectsUsingBlock:”方法。最后的代码如下所示:

    NSArray *lstAttributes = [conditionAttribute componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
        *lstValues = [conditionValue componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
        *lstValueTypes = [conditionValueType componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator];
    
    if([lstAttributes count] != [lstValues count] ||
       [lstAttributes count] != [lstValueTypes count]) return NO;
    
    NSMutableArray *lstConditions = [NSMutableArray new];
    for(uint i = 0; i < [lstAttributes count]; i ++)
    {
        SystemUIConfigurationAttributeCondition *condition = [SystemUIConfigurationAttributeCondition new];
    
        condition.conditionAttribute = [lstAttributes objectAtIndex:i];
        condition.conditionValue = [lstValues objectAtIndex:i];
        condition.conditionValueType = [lstValueTypes objectAtIndex:i];
    
        [lstConditions addObject:condition];
        FreeObject(&condition);
    }
    
    __block BOOL bResult = YES;
    [lstConditions enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *stop) 
    {
        if([self checkCondition:[lstConditions objectAtIndex:i] forOwner:owner] == NO)
        {
            bResult = NO;
            *stop = YES;
        }
    }];
    
    FreeObject(&lstConditions);
    
    return bResult;
    
    NSArray*lstAttributes=[conditionAttribute组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator],
    *lstValues=[conditionValue组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator],
    *lstValueTypes=[conditionValueType组件由字符串分隔:cstrSystemConfigurationAttributeConditionSeparator];
    如果([lstAttributes count]!=[lstValues count]||
    [lstAttributes计数]!=[lstValueTypes计数])返回编号;
    NSMUTABLEARRY*lstConditions=[NSMUTABLEARRY new];
    对于(uint i=0;i<[lsttributes count];i++)
    {
    SystemUIConfigurationAttributeCondition*条件=[SystemUIConfigurationAttributeCondition新建];
    condition.conditionAttribute=[lsttributes objectAtIndex:i];
    condition.conditionValue=[lstValues objectAtIndex:i];
    condition.conditionValueType=[lstValueTypes对象索引:i];
    [lstConditions添加对象:条件];
    自由对象(&条件);
    }
    __block BOOL bResult=是;
    [lstConditions enumerateObjectsUsingBlock:^(id obj,NSUI整数,布尔*停止)
    {
    if([self-checkCondition:[lstConditions objectAtIndex:i]forOwner:owner]==否)
    {
    b结果=否;
    *停止=是;
    }
    }];
    自由对象(条件(&L);
    返回结果;
    
    这个代码有效


    如果有人能解释我的初始循环的行为,我将不胜感激。

    我在objective-c中有一些奇怪的地方,但没有一个像那样奇怪。我只能建议在循环中的每个操作后跟踪i的值,以便查看它是否在循环中的某个地方被递减/重置为0。如果没有,那么这是for()的一个奇怪之处语句本身尝试注释此结果(&=[self-checkCondition:condition for owner:owner];您是否尝试过:
    for(int i=0;i<[lstAttributes count];i++)…
    这真的很奇怪。我会注释掉除NSLog和LSTATTERTIES赋值之外的所有内容。看看它是否仍然发生。如果它没有发生,那么一次将每一行添加回一行。啊哈。从
    检查条件:forOwner:
    返回的数据类型是什么?BOOL是有符号字符的typedef。可能按位and操作超过了b结果并在优化和架构的特定组合下踩踏i变量。你是对的,我以前也遇到过这样的错误。但目前的问题不在于这种比较(或者看起来是这样)。我在编写代码后就尝试过,代码没有&=操作。等等,我很困惑。你写道“如果我注释掉行'bResult&=”[self-checkCondition:condition for owner:owner];“然后它就工作了…”我认为这意味着在
    NSArray *lstAttributes = [conditionAttribute componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
        *lstValues = [conditionValue componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator],
        *lstValueTypes = [conditionValueType componentsSeparatedByString:cstrSystemUIConfigurationAttributeConditionSeparator];
    
    if([lstAttributes count] != [lstValues count] ||
       [lstAttributes count] != [lstValueTypes count]) return NO;
    
    NSMutableArray *lstConditions = [NSMutableArray new];
    for(uint i = 0; i < [lstAttributes count]; i ++)
    {
        SystemUIConfigurationAttributeCondition *condition = [SystemUIConfigurationAttributeCondition new];
    
        condition.conditionAttribute = [lstAttributes objectAtIndex:i];
        condition.conditionValue = [lstValues objectAtIndex:i];
        condition.conditionValueType = [lstValueTypes objectAtIndex:i];
    
        [lstConditions addObject:condition];
        FreeObject(&condition);
    }
    
    __block BOOL bResult = YES;
    [lstConditions enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *stop) 
    {
        if([self checkCondition:[lstConditions objectAtIndex:i] forOwner:owner] == NO)
        {
            bResult = NO;
            *stop = YES;
        }
    }];
    
    FreeObject(&lstConditions);
    
    return bResult;