传递给 MCPeerID 的显示名称无效

Invalid displayName passed to MCPeerID

当我尝试传递设备 udid + 设备名称时,我收到以下崩溃消息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid displayName passed to MCPeerID'

init(displayName myDisplayName: String)

来自Apple Docs

The maximum allowable length is 63 bytes in UTF-8 encoding.

The displayName parameter may not be nil or an empty string.

This method throws an exception if the displayName value is too long, empty, or nil.

在您的情况下,您传递的 udid + device name 超出了允许的限制,即 63 字节(63 个字符)

并会抛出一个异常说:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid displayName passed to MCPeerID

只是为了调试:

为您的 udid + device name 打印 lengthOfBytes,然后将其传递给 init(displayName myDisplayName: String),这肯定会超出允许的限制。

print((UIDevice.current.name+udid).lengthOfBytes(using: String.Encoding.utf8))