如何将NSUUID转换为NSData

时间:2013-10-03 23:08:20

标签: objective-c nsdata uuid nsuuid

我需要从NSData获取vendorIdentifier而不转换为NSString,清除字节。 如何使用NSUUIDgetUUIDBytes:转换为NSData

3 个答案:

答案 0 :(得分:20)

NSUUID *vendorIdentifier = [[UIDevice currentDevice] identifierForVendor];
uuid_t uuid;
[vendorIdentifier getUUIDBytes:uuid];

NSData *vendorData = [NSData dataWithBytes:uuid length:16];

答案 1 :(得分:7)

使用此选项将vendorIdentifier设为Data(Swift 4):

var uuidBytes = UIDevice.current.identifierForVendor!.uuid
let uuidData = NSData(bytes: &uuidBytes, length: 16) as Data

请注意UIDevice.current.identifierForVendor保持var uuidBytes: [UInt8] = [UInt8](count: 16, repeatedValue: 0) UIDevice.currentDevice().identifierForVendor!.getUUIDBytes(&uuidBytes) let uuidData = NSData(bytes: &uuidBytes, length: 16) 为零{/ 3}}。

原始答案(Swift 2版):

<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ToolBar"
    android:contentInsetStart="0dp"
    android:contentInsetLeft="0dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/actionBarSize"
    android:background="?android:attr/colorPrimary"
    android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
    android:popupTheme="@android:style/ThemeOverlay.Material.Light">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <ImageButton
        android:id="@+id/BackButton"
        android:layout_width="wrap_content"
        android:background="@drawable/RedButton"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:src="@drawable/back48x48"
        android:layout_alignParentLeft="true"
        style="@style/back_button_text" />
    <TextView
        android:id="@+id/AppNameView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="12dp"
        android:textSize="25dp"
        android:text="Just a Label ok" />
    <ImageButton
        android:id="@+id/MenuButton"
        android:layout_width="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_centerVertical="true"
        android:background="@drawable/RedButton"
        android:layout_alignParentRight="true"                      
        android:layout_height="wrap_content"
        android:src="@drawable/menu48x48"
        style="@style/menu_button_text" />
    </RelativeLayout>
</Toolbar>

答案 2 :(得分:1)

Swift 3版本:

let vendorId = UIDevice.current.identifierForVendor!
let uuidBytes = Mirror(reflecting: vendorId.uuid).children.map { $0.1 as! UInt8 }
let data = Data(bytes: uuidBytes)