Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Khoa Nguyen KhoaVanNguyen

🏠
Working from home
View GitHub Profile
@KhoaVanNguyen
KhoaVanNguyen / .swift
Created Apr 6, 2020
How to get list of all installed applications on MacOS with Swift 5
View .swift
var query: NSMetadataQuery? {
willSet {
if let query = self.query {
query.stop()
}
}
}
public func doSpotlightQuery() {
query = NSMetadataQuery()
View exportToPdf.cs
public string GeneratePdfWithOffice(string sourceUrl, string pdfPath, string lookupCode)
{
try
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(sourceUrl);
wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, pdfPath);
wkb.Close();
app.Quit();
View validation using guard.swift
func login() {
guard let username = usernameTF.text, isValid(username: username) else {
// wrong username format
return
}
// do register here
}
func isValid(username: String) -> Bool {
// check username trùng
View using guard demo.swift
guard DataService.ds.introImage != nil else {
showAlert(title: Settings.General.appName, message: Settings.Error.noIntroImage)
return
}
guard DataService.ds.photoImage != nil else {
showAlert(title: Settings.General.appName, message: Settings.Error.noPhotoImage)
return
}
guard DataService.ds.videoImage != nil else {
showAlert(title: Settings.General.appName, message: Settings.Error.noVideoImage)
View using guard let register user.swift
guard let username = usernameTF.text, username.characters.count >= 6 else {
// wrong username format
return
}
guard let password = passwordTF.text, password.characters.count >= 6 else {
// wrong password format
return
}
// do register user here
View using if let registering user.swift
if let username = usernameTF.text {
if let password = passwordTF.text {
// Do register user here
}else {
// Alert no password
}
}else {
// Alert no username
}
View How to reverse geocoding in IOS
func reserveGeo(){
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(userLocation, completionHandler: { (placemarks, error) in
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let formattedAddress = addressDict["FormattedAddressLines"] as? [String] {
let address = formattedAddress.joined(separator: ", ")
// set value to your control
View hide a view by touching outside of it in IOS
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
if touches.first?.view != subView{
// subView is your view
dismiss(animated: true, completion: nil)
}
}
View Variadic Parameters
```swift
func printNames( names : String... ){
for name in names{
print(name)
}
}
printNames(names: "Swift","Go","Python","Java","C++")
```
View sayHelloTo
func sayHelloTo( parameter : String){
// use parameter in function
}
sayHelloTo(argument)
You can’t perform that action at this time.