๐
This file contains hidden or 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
public extension ArraySlice { | |
private func array() -> [Element] { | |
return Array(self) | |
} | |
func withAlignedBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { | |
if startIndex == 0 || startIndex % MemoryLayout<R>.alignment == 0 { | |
return try self.withUnsafeBytes(body) | |
} else { | |
return try self.array().withUnsafeBytes(body) |
This file contains hidden or 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
public extension Array where Array.Element == UInt8 { | |
func getString(atIndex: Int, count: Int) -> String? { | |
guard let bytes = self.slice(atIndex, count: count) else { return nil } | |
guard let string = String(bytes: bytes, encoding: .utf8)?.trim() else { return nil } | |
return string | |
} | |
func getInt(atIndex: Int, count: Int) -> Int? { | |
guard let bytes = self.slice(atIndex, count: count) else { return nil } | |
let int = bytes.withAlignedBytes {$0.load(as: UInt16.self)} |
This file contains hidden or 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
extension Array where Element == UInt8 { | |
func stringEndIndex(from startIndex: Int = 0) -> Int? { | |
findStringEndIndex(from: startIndex) | |
} | |
// Method to calculate the end index of string | |
private func findStringEndIndex(from startIndex: Int, to endIndex: Int? = nil) -> Int? { | |
guard (endIndex == .none) || (endIndex != nil && endIndex! <= startIndex && endIndex! < count) | |
else { return nil } // Not a valid string |
This file contains hidden or 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
import UIKit | |
final class CustomTableViewCell: UITableViewCell { | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
commonInit() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") |
This file contains hidden or 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
import UIKit | |
final class HomeTableViewController: UITableViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.tableView.register(CustomTableViewCell.self, | |
forCellReuseIdentifier: "CustomTableViewCell") | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
OlderNewer