Skip to content

Instantly share code, notes, and snippets.

View kmuralidharan91's full-sized avatar
😀

Muralidharan Kathiresan kmuralidharan91

😀
View GitHub Profile
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 {
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")
@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
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)}
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 {
func slice(_ from: Int, count: Int) -> ArraySlice<Element>? {
guard (0 <= from && from < self.count) && (from < from+count && from+count < self.count) else {
return nil
}
return self[from...from+count-1]
}
}
switch userNotificationsAuthorizationStatus {
case .notDetermined:
requestPermission()
case .authorized, .denied, .provisional:
fallthrough
@unknown default:
// No need to request permission.
print("Didn't request permission for User Notifications")
}
switch userNotificationsAuthorizationStatus {
case .notDetermined:
requestPermission()
case .authorized, .denied, .provisional:
// No need to request permission.
print("Didn't request permission for User Notifications")
}
switch expression {
case expression1:
statement(s)
fallthrough /* optional */
case expression2, expression3: /* we can use , to use same implementation for more than one case */
statement(s)
fallthrough /* optional */
default : /* Optional */
statement(s);
}
var integer = 3
switch integer {
case 4:
print("Four.")
fallthrough
case 3:
print("Three.")
fallthrough
case 2:
print("Two.")