/ / jak zaimportować <sys / utsname.h> w swift - cel-c, swift

jak zaimportować <sys / utsname.h> w swift - target-c, swift

Tworzę projekt w Swift. Chcę wyświetlić nazwę modelu. Śledzę poniższy link, aby uzyskać modelName

http://myiosdevelopment.blogspot.co.uk/2012/11/getting-device-model-number-whether-its.html

Kod w łączu jest napisany w celu-c. Ale nie jestem pewien, jak zaimportować to do Swift.

#import <sys/utsname.h>

Proszę, pomóżcie

Odpowiedzi:

12 dla odpowiedzi № 1

sys/utsname.h jest domyślnie importowany do Swift, więc tak naprawdę nie trzeba go importować z nagłówka pomostowego. Ale używając utsname z Swift jest jednak bardzo bolesne, ponieważ Swift importuje tablicę C o stałej długości jako krotki. Jeśli spojrzysz na utsname.h, widzisz, że C struct członkowie utsname są wszyscy char tablica o długości 256:

#define _SYS_NAMELEN    256

struct  utsname {
char    sysname[_SYS_NAMELEN];  /* [XSI] Name of OS */
char    nodename[_SYS_NAMELEN]; /* [XSI] Name of this network node */
char    release[_SYS_NAMELEN];  /* [XSI] Release level */
char    version[_SYS_NAMELEN];  /* [XSI] Version level */
char    machine[_SYS_NAMELEN];  /* [XSI] Hardware type */
};

Który jest importowany do Swift w następujący sposób:

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 */
}

Tak, są to krotki z 256 Int8s. Jakie przypadki tego zabawnego autouzupełniania w Xcode:

Zakończenie Xcode inicjatora <code> utsname </code>

Obecnie nie ma możliwości zainicjowania krotkiw Swift bez zapisywania całej wartości, więc inicjowanie jej jako zmiennej lokalnej byłoby bardziej szczegółowe, jak widać powyżej. Nie ma również sposobu na przekonwertowanie krotki na tablicę, więc ogromna krotka również nie jest bardzo przydatna.

Najłatwiejszym rozwiązaniem byłoby wdrożenie go w Celu C.

Jeśli nie możesz już używać Swift, możesz to zrobić, ale to nie jest ładne:

// 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)!
}

0 dla odpowiedzi nr 2

Kod pokazany w tym poście na blogu wygląda jak C, a nie Cel C - myślę jednak, że możesz napisać to w Objective-C

Aby umożliwić mostkowanie między Objective-C i swift, po prostu dodaj nowy plik Objective-C do swojego projektu - Xcode zapyta Cię, czy utworzyć nagłówek mostkujący

wprowadź opis obrazu tutaj

Po prostu odpowiedz tak, a Xcode automatycznie utworzy plik <appname>-Bridging-Header.h plik. Otwórz i #include dowolny plik nagłówkowy celu-c, którego chcesz użyć od razu.


0 dla odpowiedzi № 3

W 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)

0 dla odpowiedzi nr 4

W Swift 4 możesz po prostu użyć właściwości modelu UIDevice:

func getPhoneModel() -> String { return UIDevice.current.model }