Skip to content

Instantly share code, notes, and snippets.

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

Wasim Alatrash dotWasim

🏠
Working from home
  • Turkey
View GitHub Profile
@dotWasim
dotWasim / gist:2462e32a29ec53b8ba4798f82cc206ee
Created January 4, 2021 10:06
Fix right-to-left ScrollView in SwiftUI
import SwiftUI
struct TestView: View {
var data = ["Test 1", "Test 2"]
var body: some View {
ScrollView(.horizontal) {
HStack{
ForEach(data, id: \.self) { name in
Text(name)
@dotWasim
dotWasim / CountWhere.swift
Created April 4, 2020 11:16
add count(where:) to Sequence protocol
extension Sequence {
func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
try reduce(0) { try predicate($1) ? $0 + 1 : $0 }
}
}
let count = [1,23,4].count(where: { $0 > 2})
print(count)
@dotWasim
dotWasim / UniqueValuesOfArray.swift
Last active February 20, 2020 12:15
Unique values of Array of items
import Foundation
let test = [1,2,3,4,1,2,2,5,20,2,1,7]
// 1- if order of items is important:
extension Sequence where Element: Hashable {
func unique() -> [Element] {
var seen: Set<Element> = []
return filter { seen.insert($0).inserted }
}
@dotWasim
dotWasim / SynchronizedSemaphore.swift
Created February 2, 2020 10:56
Thread-safe counter using DispatchSemaphore
import XCTest
struct SynchronizedSemaphore<Value> {
private let mutex = DispatchSemaphore(value: 1)
private var _value: Value
init(_ value: Value) {
self._value = value
}
@dotWasim
dotWasim / viewController.swift
Last active February 20, 2020 12:19
decode a url
//
// ViewController.swift
// Test
//
// Created by Wasim Alatrash on 1/28/20.
// Copyright © 2020 Wasim Alatrash. All rights reserved.
//
import UIKit
import SafariServices
@dotWasim
dotWasim / CompareArraysWithCommonProtocol.playground
Created January 22, 2020 12:53
Compare Arrays that implement a common Protocol
import Foundation
import XCTest
protocol Booked {
func isEqual(to other: Booked) -> Bool
}
extension Booked where Self: Equatable{
func isEqual(to other: Booked) -> Bool {
if let other = other as? Self{
@dotWasim
dotWasim / ViewController.swift
Created May 22, 2018 20:27
Crop and shadow a view by adding a new layer
//
// ViewController.swift
// ViewCropAndShadow
//
// Created by Wasim Alatrash on 5/22/18.
// Copyright © 2018 Wasim Alatrash. All rights reserved.
//
import UIKit