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
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!)
}
}
}
View OSLogExample.swift
// for example, for debugging messages in `ViewController.swift`
#if DEBUG
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ViewController")
#else
private let log = OSLog.disabled
#endif
// and then:
//
View AlamofireRequest.swift
struct Industry: Encodable {
let industryTypeCode: String
}
struct Company: Encodable {
let companyName: String
let cinNumber: String
let isListed: Bool
let industryType: [Industry]
}
View SqliteExample.swift
func retrieveArticles(searchText: String) {
precondition(db != nil, "Database not open")
articles.removeAll()
let query = "SELECT * FROM articles WHERE title = ? "
var statement: OpaquePointer?
guard sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK else {
let errmsg = String(cString: sqlite3_errmsg(db)!)
You can’t perform that action at this time.