Skip to content

Instantly share code, notes, and snippets.

View yasirmturk's full-sized avatar
💻
the smart engineer

Yasir Türk yasirmturk

💻
the smart engineer
View GitHub Profile
@yasirmturk
yasirmturk / MaxSizeCollection.swift
Created February 17, 2023 23:37
max size collection in swift like java and kotlin
@propertyWrapper
public struct MaxSizeCollection<Value: Collection> {
private let _maxSize: UInt?
public var _value: Value
public init(wrappedValue value: Value) {
_value = value
_maxSize = nil
}
@yasirmturk
yasirmturk / SnapshotContainer.swift
Last active January 14, 2022 16:08
iOS/Swift Snapshot Test utility for UIView, UITableCell, UICollectionViewCell and SwiftUI
import UIKit
final class SnapshotContainer: UIView {
let view: UIView
init(_ view: UIView, width: CGFloat, backgroundColor: UIColor = .white) {
self.view = view
super.init(frame: .zero)
@yasirmturk
yasirmturk / ModelMockable.swift
Last active December 14, 2021 19:50
Protocol to create models from json
import Foundation
final class MockableTests {
struct MockNotFound: Error {
let message: String
}
}
extension Bundle {
static let tests = Bundle(for: MockableTests.self)
@yasirmturk
yasirmturk / UIStackView+RemoveAll.swift
Created April 15, 2020 07:49
Remove all arranged subviews from UIStackView properly
import UIKit
extension UIStackView {
@discardableResult
func removeAllArrangedSubviews() -> [UIView] {
return arrangedSubviews.reduce([UIView]()) { $0 + [removeArrangedSubViewProperly($1)] }
}
func removeArrangedSubViewProperly(_ view: UIView) -> UIView {
removeArrangedSubview(view)
@yasirmturk
yasirmturk / YTMXResolver.h
Created August 12, 2014 05:49
Quick MX resolver class can be modified to resolve other DNS records like NS, PTR, TXT
//
// YTMXResolver.h
//
// Copyright 2011 Yasir M Turk. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <dns_sd.h>
@yasirmturk
yasirmturk / ExecuteOnceAtATime.m
Created July 8, 2014 07:54
Avoid multiple Executions of a method at any given time
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t renderQueue = dispatch_queue_create("com.throttling.queue", NULL);
- (void) onlyExecuteOnceAtATime {
if (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW) == 0) {
dispatch_async(renderQueue, ^{
// execution code goes here
dispatch_semaphore_signal(semaphore);
});
}
@yasirmturk
yasirmturk / HTTPLog.m
Last active December 21, 2015 10:38
Log the complete ASIHTTPRequest HTTP Request
/*
* Log the complete ASIHTTPRequest HTTP Request
*/
+ (void)logRequest:(ASIHTTPRequest *)request{
NSLog(@"url:%@", request.url);
NSLog(@"method:%@", request.requestMethod);
NSLog(@"head:%@", request.requestHeaders);
if (request.postLength > 0) {
NSLog(@"body:%@", [[NSString alloc]initWithBytes:[request.postBody bytes] length:request.postLength encoding:NSUTF8StringEncoding]);
}
//
// YTCoreDataAdapter.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
@interface YTCoreDataAdapter : NSObject {
}
@yasirmturk
yasirmturk / SingletonClass+ARC.h
Created March 5, 2013 09:03
Singleton classes is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere will return the UI…
//
// SingletonClass+ARC.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
#import <foundation/Foundation.h>
@interface SingletonClass : NSObject {
@yasirmturk
yasirmturk / gist:1408352
Created November 30, 2011 07:40
A generic core data function to save and commit our changes to DB
/*
* We can fetch a record, modify it and then call this save function to save our changes
*/
- (BOOL)saveToDb{
NSError *error = nil;
BOOL returnVal = ( [[self managedObjectContext] save:&error] );
if(error != nil)
NSLog(@"%@", error);
return returnVal;
}