Ad

Allows for things like:

import UIKit

let label = UILabel().configure {
    $0.text = "Hello World"
    $0.numberOfLines = 4
    $0.font = UIFont.systemFont(ofSize: 16)
}
import Foundation

public extension NSObjectProtocol {
    @discardableResult
    func configure(_ closure: (Self) -> Void) -> Self {
        closure(self)
        
        return self
    }
}
class Box
    attr_accessor :box, :lines, :cols
    def initialize cols, lines
        @lines = lines
        @cols = cols
        @box = []
        for line in 0...@lines do
            row = []
            for col in 0...@cols do
                isEdge = col == 0 || line == 0 || col == @cols - 1 || line == @lines - 1
                row << 'x' if isEdge
                row << ' ' unless isEdge
            end
            @box << row
        end
    end

    def printBox
        for line in 0...@lines do
            str = ''
            for col in 0...@cols do
                str << @box[line][col]
            end
            puts str
        end
    end

    def placeText text, x, y
      textLength = text.length - 1 + x
      letters = text.split ""
      for col in x..textLength do
        @box[y][col] = letters[col-x]
      end
    end
end

box = Box.new 30, 30
box.placeText "Hello World", 10, 10
box.printBox
Code
Diff
  • extension String {
      func insert(_ value: String, n: Int) -> String {
        guard n > 0 else {
          return self
        }
        var ndx = 0
        return self.reduce("") { (string, c) in
            guard ndx < n else {
                ndx = 1
                return "\(string)\(value)\(c)"
            }
            ndx += 1
            return "\(string)\(c)"
        }
      }
    }
    
    
    • func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String {
    • guard afterEveryXChars > 0 else { return intoString }
    • var output = ""
    • intoString.enumerated().forEach { index, c in
    • if index % afterEveryXChars == 0 && index > 0 {
    • output += seperator
    • extension String {
    • func insert(_ value: String, n: Int) -> String {
    • guard n > 0 else {
    • return self
    • }
    • var ndx = 0
    • return self.reduce("") { (string, c) in
    • guard ndx < n else {
    • ndx = 1
    • return "\(string)\(value)\(c)"
    • }
    • output.append(c)
    • ndx += 1
    • return "\(string)\(c)"
    • }
    • return output
    • }
    • }
    • }
Code
Diff
  • extension Int {
      var toBinary: Int {
        return (self < 0 ? -1 : 1) * (Int(String(abs(self), radix: 2)) ?? 0)
      }
    }
    • func binaryConverter(_ input: Int) -> Int {
    • //Your Code Here
    • return input
    • extension Int {
    • var toBinary: Int {
    • return (self < 0 ? -1 : 1) * (Int(String(abs(self), radix: 2)) ?? 0)
    • }
    • }
Code
Diff
  • func abbrevName(_ name: String) -> String {
        return name
            .split(separator: " ")
            .compactMap { $0.first?.uppercased() }
            .joined(separator: ".")
    }
    • func abbrevName(_ name: String) -> String {
    • let components = name.components(separatedBy: " ")
    • let left: String = String(components[0].first!)
    • return left + "." + components[1].first!.uppercased()
    • return name
    • .split(separator: " ")
    • .compactMap { $0.first?.uppercased() }
    • .joined(separator: ".")
    • }
Tips & Tricks
Code
Diff
  • func printRandomNumber(in range: ClosedRange<Int>) {
      print(Int.random(in: range))
    }
    • import Glibc
    • for count in 1...20 {
    • print(random() % 100)
    • func printRandomNumber(in range: ClosedRange<Int>) {
    • print(Int.random(in: range))
    • }