确定 iOS 设备是 32 位还是 64 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20104403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if iOS device is 32- or 64-bit
提问by Eric McNeill
Does anyone know of an easy way to tell if an iOS7 device has 32- or 64-bit hardware? I don't mean programmatically, I just mean via settings, model number, 3rd-party app, etc.
有谁知道判断 iOS7 设备是 32 位还是 64 位硬件的简单方法?我不是指以编程方式,我只是指通过设置、型号、第 3 方应用程序等。
I'm having a problem that I suspect is 64-bit related. Apple's advice is to test on the 64-bit simulator but also on an actual 64-bit device, but then doesn't say anything about how to determine that. I can write a test app to check sizeof(int) or whatever, but there's got to be some way for, say, tech support to know what they're working with.
我遇到了一个我怀疑与 64 位相关的问题。Apple 的建议是在 64 位模拟器和实际的 64 位设备上进行测试,但没有说明如何确定。我可以编写一个测试应用程序来检查 sizeof(int) 或其他什么,但必须有某种方式让技术支持知道他们正在使用什么。
Eric
埃里克
回答by Nikos M.
There is no other "official" way to determine it. You can determine it using this code:
没有其他“官方”方法可以确定它。您可以使用以下代码确定它:
if (sizeof(void*) == 4) {
NSLog(@"32-bit App");
} else if (sizeof(void*) == 8) {
NSLog(@"64-bit App");
}
回答by Dragos
Below is the method is64bitHardware. It returns YES if the hardware is a 64-bit hardware and works on a real iOS device and in an iOS Simulator. Here is source.
下面是方法是64bitHardware。如果硬件是 64 位硬件并且可以在真实的 iOS 设备和 iOS 模拟器上运行,则返回 YES。这是来源。
#include <mach/mach.h>
+ (BOOL) is64bitHardware
{
#if __LP64__
// The app has been compiled for 64-bit intel and runs as 64-bit intel
return YES;
#endif
// Use some static variables to avoid performing the tasks several times.
static BOOL sHardwareChecked = NO;
static BOOL sIs64bitHardware = NO;
if(!sHardwareChecked)
{
sHardwareChecked = YES;
#if TARGET_IPHONE_SIMULATOR
// The app was compiled as 32-bit for the iOS Simulator.
// We check if the Simulator is a 32-bit or 64-bit simulator using the function is64bitSimulator()
// See http://blog.timac.org/?p=886
sIs64bitHardware = is64bitSimulator();
#else
// The app runs on a real iOS device: ask the kernel for the host info.
struct host_basic_info host_basic_info;
unsigned int count;
kern_return_t returnValue = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count);
if(returnValue != KERN_SUCCESS)
{
sIs64bitHardware = NO;
}
sIs64bitHardware = (host_basic_info.cpu_type == CPU_TYPE_ARM64);
#endif // TARGET_IPHONE_SIMULATOR
}
return sIs64bitHardware;
}
回答by DarkDust
Totally untested, but you should be able to get the CPU via sysctl
like this:
完全未经测试,但您应该可以通过以下方式获得 CPU sysctl
:
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>
void foo() {
size_t size;
cpu_type_t type;
size = sizeof(type);
sysctlbyname("hw.cputype", &type, &size, NULL, 0);
if (type == CPU_TYPE_ARM64) {
// ARM 64-bit CPU
} else if (type == CPU_TYPE_ARM) {
// ARM 32-bit CPU
} else {
// Something else.
}
}
In the iOS 7 SDK, CPU_TYPE_ARM64
is defined in <mach/machine.h>
as:
在 iOS 7 SDK 中,CPU_TYPE_ARM64
定义<mach/machine.h>
为:
#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64)
A different way seems to be:
一种不同的方式似乎是:
#include <mach/mach_host.h>
void foo() {
host_basic_info_data_t hostInfo;
mach_msg_type_number_t infoCount;
infoCount = HOST_BASIC_INFO_COUNT;
host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
if (hostInfo.cpu_type == CPU_TYPE_ARM64) {
// ARM 64-bit CPU
} else if (hostInfo.cpu_type == CPU_TYPE_ARM) {
// ARM 32-bit CPU
} else {
// Something else.
}
}
回答by aignas
If you are compiling with clang, there is another way: just check if __arm__
or __arm64__
is defined.
如果您使用 clang 进行编译,还有另一种方法:只需检查__arm__
或__arm64__
是否已定义。
The example code below is not tested but it should illustrate what I mean by that:
下面的示例代码未经测试,但它应该说明我的意思:
#if defined(__arm__)
NSLog(@"32-bit App");
#elif defined(__arm64__)
NSLog(@"64-bit App");
#else
NSLog(@"Not running ARM");
#endif
Note that this relies on the fact that current iOS application binaries contain both, 32bit and 64bit binaries in a single container and they will be correctly selected depending on whether your app supports executing 64bit.
请注意,这取决于当前 iOS 应用程序二进制文件在单个容器中同时包含 32 位和 64 位二进制文件的事实,并且将根据您的应用程序是否支持执行 64 位正确选择它们。
回答by jherg
You can use bitWidth
on Int
https://developer.apple.com/documentation/swift/int/2885648-bitwidth
您可以bitWidth
在https://developer.apple.com/documentation/swift/int/2885648-bitwidth 上使用Int
static var is32Bit: Bool {
return Int.bitWidth == 32
}
static var is64Bit: Bool {
return Int.bitWidth == 64
}
回答by Sam Trent
I use this in swift 4, not sure if it's the best solution but it works.
我在 swift 4 中使用它,不确定它是否是最好的解决方案,但它有效。
func getCPUArch()
{
#if arch(arm)
print("this is a 32bit system")
#elseif arch(arm64)
print("this is a 64bit system")
#endif
}
回答by Andrei Pachtarou
In runtime you can use something like this
在运行时你可以使用这样的东西
extension UIDevice {
static let is64Bit = MemoryLayout<Int>.size == MemoryLayout<Int64>.size
}