インポートします:
#import <sys/types.h>
#import <sys/sysctl.h>
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
の外で関数を使用するdispatch_once
と、コンパイラはその定義をで見つけます。の2番目の引数内のブロック内で使用すると、次のdispatch_once
エラーが発生します。
No matching function for call to 'sysctlbyname'
完全なコードは次のとおりです。
#import <sys/types.h>
#import <sys/sysctl.h>
NSString *GetMachineName(void) {
static NSString *machine = nil;
if (nil == machine) {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0); // DEFINED IN SYSCTL.H
static dispatch_once_t once;
dispatch_once(&once, ^ {
// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); // DEFINED IN SYSCTL.H
// Allocate the space to store name
char *name = (char *)malloc(size);
// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);
// Place name into a string
machine = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
// Done with this
free(name);
});
if ([machine isEqualToString:@"iPhone1,1"]) return IPHONE_1G_PLATFORM;
if ([machine isEqualToString:@"iPhone1,2"]) return IPHONE_3G_PLATFORM;
if ([machine isEqualToString:@"iPhone2,1"]) return IPHONE_3GS_PLATFORM;
if ([machine isEqualToString:@"iPhone3,1"]) return IPHONE_4G_PLATFORM;
if ([machine isEqualToString:@"iPod1,1"]) return IPOD_1G_PLATFORM;
if ([machine isEqualToString:@"iPod2,1"]) return IPOD_2G_PLATFORM;
if ([machine isEqualToString:@"iPod3,1"]) return IPOD_3G_PLATFORM;
if ([machine isEqualToString:@"iPad1,1"]) return IPAD_1G_PLATFORM;
if ([machine isEqualToString:@"iPad2,1"]) return IPAD_2G_PLATFORM;
if ([machine isEqualToString:@"i386"] || [machine isEqualToString:@"x86_64"]) return SIMULATOR_PLATFORM;
return NULL;
}
return machine;
}