Skip to content

Instantly share code, notes, and snippets.

@tgnivekucn
tgnivekucn / SwiftUI_use_opaque_type_example.swift
Created November 8, 2023 07:49
SwiftUI use opaque type example
// Note: The type of body is: ModifiedContent<VStack<TupleView<(Text, Button<Text>, Text)>>, _PaddingLayout>
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world! (1)")
Button("print my type") {
print(type(of: self.body))
}
Text("Hello, world! (2)")
@tgnivekucn
tgnivekucn / opaque_type_retain_type_identity.swift
Last active November 8, 2023 07:33
Opaque type retain type identity example
public protocol Shape {
associatedtype T
func draw() -> String
}
class Triangle: Shape {
typealias T = Int
func draw() -> String {
return "Triangle"
@tgnivekucn
tgnivekucn / boxed_protocol_types_example.swift
Created November 8, 2023 06:43
The Boxed Protocol Types example
public protocol Shape {
func draw() -> String
}
struct Triangle: Shape {
typealias T = Int
var size: Int
func draw() -> String {
var result: [String] = []
@tgnivekucn
tgnivekucn / opaque_type_decided_by_function_implementation.swift
Last active November 8, 2023 06:29
Opaque type decided by function implementation
public protocol Shape {
associatedtype T
func draw() -> String
}
struct Triangle: Shape {
typealias T = Int
var size: Int
func draw() -> String {
@tgnivekucn
tgnivekucn / opaque_type_hide_information_from_module.swift
Created November 8, 2023 05:57
The Problem That Opaque Types Solve
protocol Shape {
associatedtype T
func draw() -> String
}
struct Triangle: Shape {
typealias T = Int
var size: Int
func draw() -> String {
@tgnivekucn
tgnivekucn / opaque_type_vs_boxed_type.swift
Created November 8, 2023 05:49
Opaque types v.s. Boxed types
protocol Shape {
associatedtype T
func draw() -> String
}
struct Triangle: Shape {
typealias T = Int
var size: Int
func draw() -> String {
@tgnivekucn
tgnivekucn / SetEN_US_POSIX_IN_IOS.swift
Created May 23, 2023 20:24
IOS Swift set DateFormatter and Calendar with en_US_POSIX
func toGmtDate(year: Int, month: Int, date: Int, hour: Int, minute: Int, second: Int) -> Date? {
let str = String(format: "%04d-%02d-%02d %02d:%02d:%02d+0800",
year, month, date,
hour, minute, second)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // Set locale to POSIX
if let date = dateFormatter.date(from: str) {
@tgnivekucn
tgnivekucn / ChatGPT_HTML_Test1.html
Created April 11, 2023 21:18
ChatGPT HTML Test1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>App 頁面</title>
<style>
body {
margin: 0;
display: flex;
@tgnivekucn
tgnivekucn / ChatGPT AndroidActivityTest1.java
Created April 11, 2023 21:14
ChatGPT Android Activity Test1
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@tgnivekucn
tgnivekucn / XMLTest1.xml
Created April 11, 2023 21:12
ChatGPT Generate XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"