Skip to content

Instantly share code, notes, and snippets.

@webuniverseio
Last active July 21, 2022 16:32
Show Gist options
  • Save webuniverseio/f886f0d5b5a4576d620749f3e723341a to your computer and use it in GitHub Desktop.
Save webuniverseio/f886f0d5b5a4576d620749f3e723341a to your computer and use it in GitHub Desktop.
iOS projects
import Foundation
calculator()
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input
do {
try add(n1: a, n2: b)
try subtract(n1: a, n2: b)
try multiply(n1: a, n2: b)
try divide(n1: a, n2: b)
} catch let e {
print(e.localizedDescription)
}
}
//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) throws {
print(try operation(n1, "+", n2))
}
func subtract(n1: Int, n2: Int) throws {
try print(operation(n1, "-", n2))
}
func multiply(n1: Int, n2: Int) throws {
try print(operation(n1, "*", n2))
}
func divide(n1: Int, n2: Int) throws {
try print(operation(n1, "/", n2))
}
func operation (_ x: Int, _ o: String, _ y: Int) throws -> String {
let x = Float(x), y = Float(y)
var r: Float
switch o {
case "+":
r = x + y
case "-":
r = x - y
case "*":
r = x * y
case "/":
r = x / y
default:
throw NSError(domain: "unknown operation", code: -1)
}
return skipTrailingZero(r)
}
func skipTrailingZero(_ x: Float) -> String {
let s = String(x)
return s.hasSuffix(".0") ? String(Int(x)) : s
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var diceImageViewOne: UIImageView!
@IBOutlet weak var diceImageViewTwo: UIImageView!
let variants = [#imageLiteral(resourceName: "DiceOne"), #imageLiteral(resourceName: "DiceTwo"), #imageLiteral(resourceName: "DiceThree"), #imageLiteral(resourceName: "DiceFour"), #imageLiteral(resourceName: "DiceFive"), #imageLiteral(resourceName: "DiceSix")]
override func viewDidLoad() {
super.viewDidLoad()
forEachDice {
$0?.alpha = 0.85
$0?.image = variants.last
}
}
@IBAction func ctaTouch(_ sender: UIButton) {
forEachDice { $0?.alpha -= 0.5 }
for i in 0...10 {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(i) / 10) {
self.forEachDice {
if (i != 10 ) {
$0?.image = self.getRandom()
} else {
$0?.alpha += 0.5
}
}
}
}
}
lazy var dice = [diceImageViewOne, diceImageViewTwo]
private func forEachDice(f: (UIImageView?) -> ()) {
dice.forEach { f($0) }
}
private func getRandom() -> UIImage {
let random = Int.random(in: 1..<variants.count)
return variants[random]
}
}
//there is also MVC version https://gist.github.com/webuniverseio/0f3b7975f28427ebace67ea9cf3304bd
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var bar: UIProgressView!
var seconds: Float = 0
var timer = Timer()
var initialValue: Float = 0
override func viewDidLoad() {
super.viewDidLoad()
bar.alpha = 0
}
@IBAction func onChoiceSelected(_ sender: UIButton) {
if let currentTitle = sender.currentTitle, let value = msTimers[currentTitle] {
clearTimer()
initialValue = value / 1000
bar.progress = 0
seconds = 0
bar.alpha = 1
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(setTimer), userInfo: nil, repeats: true)
}
}
@objc func setTimer() {
if seconds < initialValue {
seconds += 1
bar.progress = seconds / initialValue
} else {
bar.alpha = 0
clearTimer()
}
}
func clearTimer() {
timer.invalidate()
}
}
var msTimers: [String: Float] = [
"Soft": 5 * 1000,
"Medium": 8 * 1000,
"Hard": 12 * 1000,
]
import UIKit
// attempt #1
//var abc = [String]()
//
//var abcTemplate = "az"
//var startAndEnd: [Int] = []
//for x in abcTemplate.utf8 {
// startAndEnd.append(Int(x))
//}
//
//for i in startAndEnd[0]...startAndEnd[1] {
// let char = String(UnicodeScalar(i)!)
// abc.append(char)
//}
// attempt #2
//var start: Int = -1
//for x in "a".utf8 {
// start = Int(x)
//}
var start = Int("a".unicodeScalars.first!.value)
let abcCount = 26
var abc = Array(0..<abcCount).map { String(UnicodeScalar($0 + start)!) }
abc.shuffle()
print(abc[0...7].joined(separator: ""))
import UIKit
import AVFoundation
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func touchHandler(_ sender: UIButton) {
if let title = sender.currentTitle {
playSound(title)
}
sender.alpha = 0.5
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
sender.alpha = 1
}
}
var player: AVAudioPlayer?
func playSound(_ resource: String) {
do {
guard let url = Bundle.main.url(forResource: resource, withExtension: "wav") else {
throw NSError(domain: "\(resource) can't be found", code: Errors.noFile.rawValue)
}
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else {
throw NSError(domain: "player cant start", code: Errors.noPlayer.rawValue)
}
player.play()
} catch let e {
print(e.localizedDescription)
}
}
}
enum Errors: Int {
case noFile = 1000
case noPlayer = 1001
}
@webuniverseio
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment