├── .gitignore ├── LICENSE ├── NSData+Byte.swift ├── README.md └── UInt8+Bit.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Necessary shitz in swift 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /NSData+Byte.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NSData+Byte.swift 3 | // 4 | // 5 | // Created by Joey Z on 12/28/15. 6 | // Github.com/Swift-Kit 7 | // Github.com/josephyzhou 8 | // 9 | 10 | import Foundation 11 | 12 | extension NSData { 13 | 14 | // turning NSData into an array of bytes 15 | var byteArray : [UInt8] { 16 | 17 | // create array of appropriate length 18 | let count = self.length / sizeof(UInt8) 19 | var array = [UInt8](count: count, repeatedValue: 0) 20 | 21 | // copy bytes into array 22 | self.getBytes(&array, length:count * sizeof(UInt8)) 23 | 24 | // return the array 25 | return array 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JZBitByteNSData 2 | NSData with byte/bit support 3 | -------------------------------------------------------------------------------- /UInt8+Bit.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UInt8+Bit.swift 3 | // 4 | // 5 | // Created by Joey Z on 12/28/15. 6 | // Github.com/Swift-Kit 7 | // Github.com/josephyzhou 8 | // 9 | 10 | import Foundation 11 | 12 | extension UInt8 { 13 | 14 | // turning UInt8 into an array of Bitss 15 | var bitArray : [Bit] { 16 | 17 | // create array of appropriate length: 18 | var array = [Bit](count: 8, repeatedValue: .Zero) 19 | 20 | // copy bytes into array 21 | var byteStr = String(self, radix:2) 22 | 23 | // pad the string with 0s 24 | let diff = 8 - byteStr.length 25 | if diff > 0 { 26 | for _ in 0..