Skip to content

Instantly share code, notes, and snippets.

View IhwanID's full-sized avatar

Ihwan IhwanID

View GitHub Profile
@IhwanID
IhwanID / main.swift
Created March 15, 2021 01:22
For Where in Swift
let numbers = [1, 2, 3 , 4 , 5, 6]
for number in numbers{
if number.isMultiple(of: 2){
print(number)
}
}
for number in numbers where number.isMultiple(of: 2){
print(number)
@IhwanID
IhwanID / Optional+Ext.swift
Created March 15, 2021 01:16
String OrEmpty Optional Extension
extension Optional where Wrapped == String{
var orEmpty: String{
switch self{
case .some(let value):
return value
case .none:
return ""
}
}
}
@IhwanID
IhwanID / main.swift
Last active February 5, 2021 15:26
Combine Many Merge Pokemon API
//
// ViewController.swift
// Pokemon
//
// Created by Ihwan ID on 04/02/21.
//
import UIKit
import Combine
@IhwanID
IhwanID / main.swift
Created January 31, 2021 06:26
Two Sum in Swift
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dictionary = [Int:Int]();
for index in 0 ..< nums.count {
let complement = target - nums[index];
if dictionary.keys.contains(complement) && dictionary[complement] != index {
return [dictionary[complement]!,index];
}
dictionary[nums[index]] = index
}
@IhwanID
IhwanID / main.swift
Created January 30, 2021 15:49
Get Most Common Element in Array
var colorArray = ["blue", "white", "yellow", "red","blue", "white", "yellow", "red","blue", "white", "yellow", "red","blue", "white", "yellow", "red","blue", "white", "yellow", "red","yellow", "red","yellow", "red","yellow", "red", "red"]
func getMostColor(input: [String]) -> String{
var topColor: String = ""
var colorDict: [String:Int] = [:]
for color in input{
colorDict[color, default:0]+=1
}
@IhwanID
IhwanID / MotivasiHarianWidget.swift
Created January 13, 2021 09:26
Daily Motivation WidgetKit SwiftUI
//
// MotivasiHarianWidget.swift
// MotivasiHarianWidget
//
// Created by Ihwan ID on 13/01/21.
//
import WidgetKit
import SwiftUI
import Intents
@IhwanID
IhwanID / ContentView.swift
Created January 10, 2021 01:25
Create Tab Bar in SwiftUI
struct ContentView: View {
var body: some View {
TabView {
Text("Home Page")
.tabItem {
Image(systemName: "house.fill")
Text("Homeb")
}
Text("Favorite Page")
.tabItem {
@IhwanID
IhwanID / ContentView.swift
Last active January 10, 2021 01:13
SwiftUI List
struct ContentView: View {
var body: some View {
let rangga = Student(name: "Rangga", age: 18)
let andi = Student(name: "Andi",age: 19)
let ihwan = Student(name: "Ihwan", age: 20)
let students = [rangga, andi, ihwan]
return List(students) { student in
StudentRow(student: student)
}
@IhwanID
IhwanID / main.swift
Created January 9, 2021 16:49
Example Using Webview In SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
SwiftUIWebView(url: URL(string: "https://ihwan.id"))
}
}
}
@IhwanID
IhwanID / main.swift
Created January 9, 2021 16:24
Tab Bar SwiftUI
struct ContentView: View {
var body: some View {
TabView {
Text("Home Page")
.tabItem {
Image(systemName: "house.fill")
Text("Homeb")
}
Text("Favorite Page")
.tabItem {