Last active
February 7, 2025 07:01
-
-
Save bpolania/704901156020944d3e20fef515e73d61 to your computer and use it in GitHub Desktop.
Swift Extensions for Data, Int, UInt8, UInt16, and UInt32 types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) 2018 Boris Polania | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
// DataExtensions.swift | |
// | |
// Created by Boris Polania on 2/16/18. | |
// | |
import UIKit | |
extension Data { | |
var uint8: UInt8 { | |
get { | |
var number: UInt8 = 0 | |
self.copyBytes(to:&number, count: MemoryLayout<UInt8>.size) | |
return number | |
} | |
} | |
var uint16: UInt16 { | |
get { | |
let i16array = self.withUnsafeBytes { $0.load(as: UInt16.self) } | |
return i16array | |
} | |
} | |
var uint32: UInt32 { | |
get { | |
let i32array = self.withUnsafeBytes { $0.load(as: UInt32.self) } | |
return i32array | |
} | |
} | |
var uuid: NSUUID? { | |
get { | |
var bytes = [UInt8](repeating: 0, count: self.count) | |
self.copyBytes(to:&bytes, count: self.count * MemoryLayout<UInt32>.size) | |
return NSUUID(uuidBytes: bytes) | |
} | |
} | |
var stringASCII: String? { | |
get { | |
return NSString(data: self, encoding: String.Encoding.ascii.rawValue) as String? | |
} | |
} | |
var stringUTF8: String? { | |
get { | |
return NSString(data: self, encoding: String.Encoding.utf8.rawValue) as String? | |
} | |
} | |
struct HexEncodingOptions: OptionSet { | |
let rawValue: Int | |
static let upperCase = HexEncodingOptions(rawValue: 1 << 0) | |
} | |
func hexEncodedString(options: HexEncodingOptions = []) -> String { | |
let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx" | |
return map { String(format: format, $0) }.joined() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) 2018 Boris Polania | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
// DataExtensions.swift | |
// | |
// Created by Boris Polania on 2/16/18. | |
// | |
// | |
// IntsExtensions.swift | |
// | |
// Created by Boris Polania on 2/19/18. | |
// | |
import UIKit | |
extension Int { | |
var data: Data { | |
var int = self | |
return Data(bytes: &int, count: MemoryLayout<Int>.size) | |
} | |
} | |
extension UInt8 { | |
var data: Data { | |
var int = self | |
return Data(bytes: &int, count: MemoryLayout<UInt8>.size) | |
} | |
} | |
extension UInt16 { | |
var data: Data { | |
var int = self | |
return Data(bytes: &int, count: MemoryLayout<UInt16>.size) | |
} | |
} | |
extension UInt32 { | |
var data: Data { | |
var int = self | |
return Data(bytes: &int, count: MemoryLayout<UInt32>.size) | |
} | |
var byteArrayLittleEndian: [UInt8] { | |
return [ | |
UInt8((self & 0xFF000000) >> 24), | |
UInt8((self & 0x00FF0000) >> 16), | |
UInt8((self & 0x0000FF00) >> 8), | |
UInt8(self & 0x000000FF) | |
] | |
} | |
} | |
Author
bpolania
commented
Nov 25, 2019
via email
Hello John!
Yes, I did a while ago, I even forgot about it. I will update it for Swift 5
Cheers,
B.
…On Mon, Nov 25, 2019 at 5:46 AM John Brohan ***@***.***> wrote:
Hi Boris.
This code is very helpful. However it gives an error in Swift5.
'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_:
(UnsafeRawBufferPointer) throws -> R) rethrows -> R instead
in the function var uint16: UInt16 {
get {
let i16array = self.withUnsafeBytes {
UnsafeBufferPointer(start: $0, count:
self.count/2).map(UInt16.init(littleEndian:))
}
return i16array[0]
}
}
I appreciate very much your effort to hide away this ugly construct to
give the programmer a natural view of Data.
John Brohan
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/704901156020944d3e20fef515e73d61?email_source=notifications&email_token=AAG3M7TPUHNHFGWBPX43MWLQVPJMLA5CNFSM4JRJFEUKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF4YCS#gistcomment-3092521>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAG3M7SADEXH6OP3JDHHZ3LQVPJMLANCNFSM4JRJFEUA>
.
Did this get updated for swift 5.5? It still has withUsafeBytes which is deprecated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment