Skip to content

Instantly share code, notes, and snippets.

View kmuralidharan91's full-sized avatar
๐Ÿ˜€

Muralidharan Kathiresan kmuralidharan91

๐Ÿ˜€
View GitHub Profile
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)
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)}
@kmuralidharan91
kmuralidharan91 / StringEndIndex.swift
Last active April 14, 2020 12:59
StringEndIndex
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
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")
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 {