Skip to content

Instantly share code, notes, and snippets.

View sunilsharma08's full-sized avatar
🏠
Working from home

Sunil Sharma sunilsharma08

🏠
Working from home
View GitHub Profile
@nyg
nyg / MemoryAddress.swift
Last active July 9, 2024 03:38
Get the memory address of both class and structure instances in Swift.
// https://stackoverflow.com/a/45777692/5536516
import Foundation
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
// MyURLStreamTask.swift
// Demonstrates using an NSURLSessionStreamTask to implement a bidirectional TCP socket connection
//
// by [email protected] 2017-03-07
// distribution: BSD 2-clause
//
import Foundation
class MyURLStreamTask {
exec > /tmp/${PROJECT_NAME}_archive.log 2>&1
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"
@sgr-ksmt
sgr-ksmt / file.swift
Created September 26, 2016 05:55
Safe DispatchQueue.main.sync (Swift3.0)
extension DispatchQueue {
class func mainSyncSafe(execute work: () -> Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.sync(execute: work)
}
}
class func mainSyncSafe<T>(execute work: () throws -> T) rethrows -> T {
@Sorix
Sorix / UIView+round.swift
Created August 24, 2016 18:39
Round specified corners of UIView
// Example: view.round([.TopLeft, .TopRight], radius: 15)
extension UIView {
/**
Rounds the given set of corners to the specified radius
- parameter corners: Corners to round
- parameter radius: Radius to round to
*/
func round(corners: UIRectCorner, radius: CGFloat) {
@ferbass
ferbass / button.swift
Created May 3, 2016 04:14
Adding a closure as target to a UIButton
import UIKit
extension UIButton {
private func actionHandleBlock(action:(() -> Void)? = nil) {
struct __ {
static var action :(() -> Void)?
}
if action != nil {
@aufflick
aufflick / AssociationHelpers.swift
Last active November 6, 2021 06:55
Helpers to assist using the ObjC runtime to associate both objects and structs in Swift
// Associated wrapper by WeZZard : https://wezzard.com/2015/10/09/associated-object-and-swift-struct/
// Type safe helpers inspired by Tikitu de Jager : https://medium.com/@ttikitu/swift-extensions-can-add-stored-properties-92db66bce6cd#.mx6ekrw16
public final class AssociatedStruct<T>: NSObject, NSCopying {
public typealias Type = T
public let value: Type
public init(_ value: Type) { self.value = value }
internal let DEFAULT_MIME_TYPE = "application/octet-stream"
internal let mimeTypes = [
"html": "text/html",
"htm": "text/html",
"shtml": "text/html",
"css": "text/css",
"xml": "text/xml",
"gif": "image/gif",
"jpeg": "image/jpeg",
@marchinram
marchinram / UIImage+PixelColor.swift
Last active January 19, 2022 08:53
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
guard x >= 0 && x < Int(size.width) && y >= 0 && y < Int(size.height),
let cgImage = cgImage,
let provider = cgImage.dataProvider,
let providerData = provider.data,
let data = CFDataGetBytePtr(providerData) else {
return nil
//
// FileUploader.swift
// ERAssistant
//
// Created by Narciso Cerezo Jiménez on 27/5/15.
// Copyright (c) 2015 Closure Software. All rights reserved.
//
import Foundation
import Alamofire