Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
执行[[NSString alloc]initWithFormat:format参数:argList]时iOS8 EXC\u错误\u访问_Ios8_Xcode6 - Fatal编程技术网

执行[[NSString alloc]initWithFormat:format参数:argList]时iOS8 EXC\u错误\u访问

执行[[NSString alloc]initWithFormat:format参数:argList]时iOS8 EXC\u错误\u访问,ios8,xcode6,Ios8,Xcode6,以下代码在iOS7上运行良好。当我在Xcode6/iOS上运行时,它崩溃了 + (void)log:(NSString *)format arguments:(va_list)argList { NSLogv(format, argList); if ([self sharedConsole].enabled) { NSString *message = [[NSString alloc] initWithFormat:format argumen

以下代码在iOS7上运行良好。当我在Xcode6/iOS上运行时,它崩溃了

+ (void)log:(NSString *)format arguments:(va_list)argList
{   
    NSLogv(format, argList);

    if ([self sharedConsole].enabled)
    {
        NSString *message = [[NSString alloc] initWithFormat:format arguments:argList]; //Crash here with info of EXC_BAD_ACCESS
        if ([NSThread currentThread] == [NSThread mainThread])
        {   
            [[self sharedConsole] logOnMainThread:message];
        }
        else
        {
            [[self sharedConsole] performSelectorOnMainThread:@selector(logOnMainThread:)
                                                   withObject:message waitUntilDone:NO];
        }
    }
}
我也有同样的问题

NSLogv在iOS8上的实施更改

我的解决方案:关闭va_列表,并在每次使用之间重新添加

+ (void)log:(NSString *)format arguments:(va_list)argList
{   
    va_start(argList,format) ;
    NSLogv(format, argList);
    va_end(afgList) ;

    va_start(argList,format) ;
    if ([self sharedConsole].enabled)
    {
        NSString *message = [[NSString alloc] initWithFormat:format arguments:argList]; //Crash here with info of EXC_BAD_ACCESS
        if ([NSThread currentThread] == [NSThread mainThread])
        {   
            [[self sharedConsole] logOnMainThread:message];
        }
        else
        {
            [[self sharedConsole] performSelectorOnMainThread:@selector(logOnMainThread:)
                                                   withObject:message waitUntilDone:NO];
        }
    }
    va_end(afgList) ;
}

正如User24230指出的,必须重新初始化参数列表。如果您的方法是可变的,您可以使用va_start和va_end。但是,您似乎正在初始化并从外部传递一个va_列表。在这种情况下,您可以如下方式复制列表:

+ (void)log:(NSString *)format arguments:(va_list)argList
{
    va_list internalArgs;

    va_copy(internalArgs, argList);
    NSLogv(format, internalArgs);
    va_end(internalArgs);

    va_copy(internalArgs, argList);
    if ([self sharedConsole].enabled)
    {
        NSString *message = [[NSString alloc] initWithFormat:format arguments:internalArgs];
        if ([NSThread currentThread] == [NSThread mainThread])
        {   
            [[self sharedConsole] logOnMainThread:message];
        }
        else
        {
            [[self sharedConsole] performSelectorOnMainThread:@selector(logOnMainThread:)
                                                   withObject:message waitUntilDone:NO];
        }
    }
    va_end(internalArgs);
}

initWithFormat:arguments:崩溃最常见的情况是格式字符串包含的参数多于指定的参数