Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
Objective c 如何导入<;系统/utsname.h>;迅速地_Objective C_Swift - Fatal编程技术网

Objective c 如何导入<;系统/utsname.h>;迅速地

Objective c 如何导入<;系统/utsname.h>;迅速地,objective-c,swift,Objective C,Swift,我正在用Swift创建一个项目。我想显示modelName。我按照下面的链接获取模型名 http://myiosdevelopment.blogspot.co.uk/2012/11/getting-device-model-number-whether-its.html 链接中的代码是用objective-c编写的。但我不知道如何在Swift中导入此项 #import <sys/utsname.h> #导入 请有人帮忙那篇博文中显示的代码看起来像C,而不是Objective C

我正在用Swift创建一个项目。我想显示modelName。我按照下面的链接获取模型名

http://myiosdevelopment.blogspot.co.uk/2012/11/getting-device-model-number-whether-its.html
链接中的代码是用objective-c编写的。但我不知道如何在Swift中导入此项

#import <sys/utsname.h>
#导入

请有人帮忙

那篇博文中显示的代码看起来像C,而不是Objective C——不过我认为你可以用Objective-C编写一个包装器

为了在Objective-C和swift之间启用桥接,只需在项目中添加一个新的Objective-C文件-Xcode将提示您是否创建桥接头


只要回答是,Xcode就会自动创建一个
-bridgeing Header.h
文件。打开它,并包含要从swift使用的任何objective-c头文件。

sys/utsname.h
默认导入swift,因此实际上不需要从桥接头导入它。但是使用Swift的
utsname
确实很痛苦,因为Swift将固定长度的C数组作为元组导入。如果查看
utsname.h
,您会发现
utsname
的C
struct
成员都是长度为256的
char
数组:

定义系统名称256
结构utsname{
char sysname[_SYS_NAMELEN];/*[XSI]操作系统的名称*/
char nodename[_SYS_NAMELEN];/*[XSI]此网络节点的名称*/
字符释放[_SYS_NAMELEN];/*[XSI]释放级别*/
字符版本[_SYS_NAMELEN];/*[XSI]版本级别*/
字符机器[_SYS_NAMELEN];/*[XSI]硬件类型*/
};
将其导入Swift,如下所示:

var _SYS_NAMELEN: Int32 { get }

struct utsname {
    var sysname: (Int8, Int8, /* ... 254 more times "Int8, " here ... */) /* [XSI] Name of OS */
    var nodename: (Int8, Int8, /* ... snip ... */ ) /* [XSI] Name of this network node */
    var release: (Int8, Int8, /* ... snip ... */ ) /* [XSI] Release level */
    var version: (Int8, Int8, /* ... snip ... */ ) /* [XSI] Version level */
    var machine: (Int8, Int8, /* ... snip ... */ ) /* [XSI] Hardware type */
}
是的,它们是256
Int8
s的元组。在哪些情况下,Xcode中会出现这种滑稽的自动完成:

utsname初始值设定项“>

目前,在Swift中无法在不写出所有值的情况下初始化元组,因此,如上所述,将其初始化为局部变量将相当繁琐。也无法将元组转换为数组,因此庞大的元组也不是很有用

最简单的解决方案是在Objective-C中实现它。

如果你下定决心要使用Swift,你可以这样做,但这并不漂亮:

// Declare an array that can hold the bytes required to store `utsname`, initilized
// with zeros. We do this to get a chunk of memory that is freed upon return of
// the method
var sysInfo: [CChar] = Array(count: sizeof(utsname), repeatedValue: 0)

// We need to get to the underlying memory of the array:
let machine = sysInfo.withUnsafeMutableBufferPointer { (inout ptr: UnsafeMutableBufferPointer<CChar>) -> String in
    // Call uname and let it write into the memory Swift allocated for the array
    uname(UnsafeMutablePointer<utsname>(ptr.baseAddress))

    // Now here is the ugly part: `machine` is the 5th member of `utsname` and
    // each member member is `_SYS_NAMELEN` sized. We skip the the first 4 members
    // of the struct which will land us at the memory address of the `machine`
    // member
    let machinePtr = advance(ptr.baseAddress, Int(_SYS_NAMELEN * 4))

    // Create a Swift string from the C string
    return String.fromCString(machinePtr)!
}
//声明一个数组,该数组可以保存存储“utsname”所需的字节,已初始化
//使用零。我们这样做是为了获得一块在返回时释放的内存
//方法
var sysInfo:[CChar]=数组(计数:sizeof(utsname),repeatedValue:0)
//我们需要访问阵列的底层内存:
让machine=sysInfo.withUnsafeMutableBufferPointer{(inout ptr:UnsafeMutableBufferPointer)->中的字符串
//调用uname并让它写入为数组分配的内存
uname(不可配置指针(ptr.baseAddress))
//现在是丑陋的部分:'machine'是'utsname'的第五个成员
//每个成员的大小都是“\u SYS\u NAMELEN”。我们跳过前4个成员
//将把我们放在“机器”的内存地址的结构`
//成员
设machinePtr=advance(ptr.baseAddress,Int(_SYS_NAMELEN*4))
//从C字符串创建Swift字符串
返回字符串.fromCString(machinePtr)!
}
在swift 2.0中:

var sysInfo: [CChar] = Array(count: sizeof(utsname), repeatedValue: 0)
let deviceModel = sysInfo.withUnsafeMutableBufferPointer { (inout ptr: UnsafeMutableBufferPointer<CChar>) -> String in
        uname(UnsafeMutablePointer<utsname>(ptr.baseAddress))
        let machinePtr = ptr.baseAddress.advancedBy(Int(_SYS_NAMELEN * 4))
        return String.fromCString(machinePtr)!
}
print(deviceModel)
var sysInfo:[CChar]=数组(计数:sizeof(utsname),repeatedValue:0)
让deviceModel=sysInfo.WithUnsafemtableBufferPointer{(inout ptr:UnsafemtableBufferPointer)->输入字符串
uname(不可配置指针(ptr.baseAddress))
设machinePtr=ptr.baseAddress.advancedBy(Int(_SYS_NAMELEN*4))
返回字符串.fromCString(machinePtr)!
}
打印(设备模型)

在Swift 4中,您只需使用UIDevice model属性即可:

func getPhoneModel()->字符串{
返回UIDevice.current.model

}

我已经创建了-bridgeing Header.h文件,并在其中添加了#include。之后在ViewController.swift类中,我尝试使用这个-struct utsname systemInfo。但是它会给我一个错误,在struct中说expected“{”,如果可以的话……我从未尝试或阅读过C和swift之间的互操作性:)。如果这不起作用(事实上我希望如此),只需将C代码包装在objective-C类方法中,然后使该类可用于swiftI Truse String。如果“机器”名称长度为256个字符,fromCString()将失败(并崩溃)。@WilShipley
let machine=sysInfo.WithUnsafemtableBufferPointer{(inout ptr:UnsafemtableBufferPointer)->uname中的字符串(unsafemtablepointer(ptr.baseAddress))让machinePtr=ptr.baseAddress.advancedBy(Int(_SYS_NAMELEN*4))var buf:[CChar]=Array(count:Int(_SYS_NAMELEN)+1,repeatedValue:0);返回buf.withunsafemtablebufferpointer({(inout bufPtr:unsafemtablebufferpointer)->strncpy(bufPtr.baseAddress,machinePtr,Int(_SYS_NAMELEN))中的字符串)返回字符串.fromCString(bufPtr.baseAddress)!}