Skip to content

Instantly share code, notes, and snippets.

Rob Ryan robertmryan

Block or report user

Report or block robertmryan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@robertmryan
robertmryan / String+IntegerIndex.swift
Created Dec 11, 2019
Extension to `String` to allow numeric indexes for ranges
View String+IntegerIndex.swift
extension String {
public subscript(range: Range<String.IndexDistance>) -> Substring {
let lowerBound = index(startIndex, offsetBy: range.lowerBound)
let upperBound = index(startIndex, offsetBy: range.upperBound)
return self[lowerBound..<upperBound]
}
public subscript(range: ClosedRange<String.IndexDistance>) -> Substring {
let lowerBound = index(startIndex, offsetBy: range.lowerBound)
let upperBound = index(startIndex, offsetBy: range.upperBound)
View CancelDispatchWorkItemViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var button: UIButton!
var isStart = false
var work: DispatchWorkItem?
private func getCurrentTime() -> DispatchWorkItem {
return DispatchWorkItem { [weak self] in
while self?.work?.isCancelled == false {
View Example.swift
let group = DispatchGroup()
for album in albums {
let formattedAlbum = AlbumFormatted(context: self.persistenceManager.context)
formattedAlbum.albumName = album.name
group.enter()
self.getTracklist(album: album.id!) { data in
// do whatever you need to add this `data` to your temporary result set
group.leave()
}
View Example.swift
struct Cost: Codable {
let id: String
let label: String
let value: String
}
let cost = Cost(id: "something", label: "something", value: "something")
let jsonData = try! JSONEncoder().encode(cost) // this works fine
let object = try! JSONSerialization.jsonObject(with: jsonData) // this works fine
View Synchron.m
@interface Synchron: NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *identifier;
- (id)initWithName:(NSString *)name identifier:(NSString *)identifier;
+ (instancetype)synchronWithName:(NSString *)name identifier:(NSString *)identifier;
@end
View SnapshotWithMarkerView.swift
func generateMap(with coordinates: [CLLocationCoordinate2D]) {
var annotationViews = [MKMarkerAnnotationView]()
let startCoord = coordinates.first!
let endCoord = coordinates.last!
annotationViews.append(createMapMarkerAnnotation(text: "A", coordinates: startCoord, color: .green))
annotationViews.append(createMapMarkerAnnotation(text: "B", coordinates: endCoord, color: .green))
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.start { snapshot, error in
View QuadBezier.swift
class ViewController: UIViewController {
var bezier: QuadBezier!
let pathLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.lineWidth = 5
layer.strokeColor = UIColor.blue.cgColor
layer.fillColor = UIColor.clear.cgColor
return layer
View AsyncTest.swift
class MyAppTests: XCTestCase {
func testIets() {
let expectation = self.expectation(description: "doSomething")
AsyncClass().doSomething {
expectation.fulfill()
}
wait(for: [expectation], timeout: 5)
}
View Iso8601.swift
// Personally, I would have done this on `DateFormatter` extension, but
// did it on Formatter so it would work with provided code snippet.
extension Formatter {
static var iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
return formatter
View RemoveAnnotations.swift
func removeAllAnnotations() {
guard let document = document else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
while page.annotations.count > 0 {
page.removeAnnotation(page.annotations.last!)
}
}
}
You can’t perform that action at this time.