Skip to content

Instantly share code, notes, and snippets.

View mahmudahsan's full-sized avatar

Mahmud Ahsan mahmudahsan

View GitHub Profile
@mahmudahsan
mahmudahsan / error_handling3.swift
Created December 22, 2017 16:41
Error handling in swift
do {
try sampleThrowError()
}
catch SampleError.sample1 {
print("Sample 1 Error caught")
}
catch {
print("For all other cases")
}
@mahmudahsan
mahmudahsan / error_handling4.swift
Created December 22, 2017 16:41
Error handling in swift
let str = try? sampleThrowError()
if let str = str {
print(str)
}
else {
print("Str is nil")
}
@mahmudahsan
mahmudahsan / error_handling5.swift
Created December 22, 2017 16:43
Error handling in swift
func square(val:Int) throws -> Int {
if val == 5 {
throw SampleError.sample1
}
return val * val
}
let sqaureValue = try! square(val: 10)
print(sqaureValue)
@mahmudahsan
mahmudahsan / defer.swift
Created December 22, 2017 16:44
cleanup actions using defer
func readDesc(_ desc:inout String) throws {
let newDesc = desc
defer {
desc = ""
}
throw SampleError.sample2(message: "cool error")
print(newDesc)
}
var desc = "Life is cool and play is a tool!"
def parse_soup_to_simple_html(self):
news_list = self.__soup.find_all(['h1', 'h2']) # h1
#print (news_list)
htmltext = '''
<html>
<head><title>Simple News Link Scrapper</title></head>
<body>
{NEWS_LINKS}
@mahmudahsan
mahmudahsan / obj-c.m
Last active February 7, 2020 05:26
Objective-C Code
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *word = [NSString stringWithUTF8String:(char *)sqlite3_column_tcompiledStatement,
[arrayWords addObject:word];
}
}
@mahmudahsan
mahmudahsan / swiftui_gradient_text1.swift
Created February 27, 2020 04:40
SwiftUI How to create a gradient text
import SwiftUI
extension Color {
static func hexStringToColor (hex:String, opacity: Double = 1.0) -> Color {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return Color.gray
@mahmudahsan
mahmudahsan / swiftui_color_from_hex.swift
Created February 27, 2020 04:42
SwiftUI how to create color from hex value
extension Color {
static func hexStringToColor (hex:String, opacity: Double = 1.0) -> Color {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return Color.gray
}
var rgbValue:UInt64 = 0
struct GradientText: View {
let title: String
let colors = Gradient(colors: [Color.hexStringToColor(hex: "#FF0000"),
Color.hexStringToColor(hex: "#40B482")])
var body: some View {
RadialGradient(gradient: colors,
center: .center,
startRadius: 0,
endRadius: 300)
import SwiftUI
//MARK:- Checkbox Field
struct CheckboxField: View {
let id: String
let label: String
let size: CGFloat
let color: Color
let textSize: Int
let callback: (String, Bool)->()