检测iOS设备上的CPU核心

jar*_*ryd 4 objective-c grand-central-dispatch ios

我正在编写一些代码dispatch_async并在iphone 4s和ipad 1st gen上得到不同的结果.

我想知道是否是由于CPU的核心数量.是否有可能在运行时检测iOS设备的核心数或CPU类型,这样我可以dispatch_async在4s,但不能在ipad上?

Sim*_*ain 7

以下是检测iOS设备上核心数量的代码:

#include <sys/sysctl.h>

unsigned int countCores()
{
    size_t len;
    unsigned int ncpu;

    len = sizeof(ncpu);
    sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

    return ncpu;
}
Run Code Online (Sandbox Code Playgroud)

除此之外,您还可以检查[[UIDevice currentDevice] userInterfaceIdiom]以确定该设备是iPhone还是iPad.像这样:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    NSLog(@"iPad");
}
else {
    NSLog(@"iPhone");
}
Run Code Online (Sandbox Code Playgroud)

参考