SWIFT 5 / XCODE 프로젝트에서 몇 가지 경고를 정리하려고하는데이 문제가 해결되지 않았습니다.
let sendBytes:[UInt8] = [0x0, 0x0, 0x5, 0x0]
let msgData = Data(bytes: UnsafePointer<UInt8>(sendBytes), count: sendBytes.count)
socket.write(msgData, withTimeout: -1.0, tag: 0)
socket.readData(withTimeout: -1.0, tag: 0)
"UnsafePointer"에 대해 다음과 같은 경고와 두 가지 제안이 표시됩니다.
'UnsafePointer'를 초기화하면 포인터가 매달려 있습니다.
'[UInt8]'에서 'UnsafePointer'로의 암시 적 인수 변환은 'init (_ :)'에 대한 호출 기간 동안 만 유효한 포인터를 생성합니다.
인수를 정의 된 범위에 유효한 버퍼 포인터로 명시 적으로 변환하려면 Array에서 'withUnsafeBufferPointer'메서드를 사용합니다.
이것이 내 해결책입니다.
버전 1 :
let sendBytes:[UInt8] = [0x0, 0x0, 0x5, 0x0]
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: sendBytes.count)
uint8Pointer.initialize(from: sendBytes, count: sendBytes.count)
let msgData = Data(bytes: uint8Pointer, count: sendBytes.count)
socket.write(msgData, withTimeout: -1.0, tag: 0)
socket.readData(withTimeout: -1.0, tag: 0)
버전 2 :
let sendBytes:[UInt8] = [0x0, 0x0, 0x5, 0x0]
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: sendBytes.count)
uint8Pointer.initialize(from: sendBytes, count: sendBytes.count)
let msgData = Data(bytes: uint8Pointer, count: sendBytes.count)
socket.write(msgData, withTimeout: -1.0, tag: 0)
socket.readData(withTimeout: -1.0, tag: 0)
uint8Pointer.deallocate()
Swift 3부터는 배열 로 Data
인스턴스 를 간단히 초기화 할 수 있습니다 UInt8
.
let sendBytes:[UInt8] = [0x0, 0x0, 0x5, 0x0]
let msgData = Data(sendBytes)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다