Skip to content

Instantly share code, notes, and snippets.

@flaviodsilverio
flaviodsilverio / gist:b806d35f14c2e4e285790680a88f2ea9
Last active November 14, 2023 15:45
Mastering Arrays in Swift
var arrayToMaster = [7, 2, 10, 4, 5]
arrayToMaster.sort(by: <)
print(arrayToMaster)
arrayToMaster.sort { s1, s2 in
s1 > s2
}
- (void) URLRequestObjCParaOCDN {
//descomenta isto ---> //__weak "nomeDaClasseOndeTensIsto" *weakSelf = self; //nao uses self. em lado nenhum no block, usa weakSelf. nao pode originar leaks e é igual #proTip
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"o teu link aqui"]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
//Ele so executa isto quando o request e finalizado, isto e executado numa background thread. Se aqui dentro precisares de usar um "self." diz-me que ensino-te a fazer um weakify ao self para nao teres um memory leak. O "error" so traz erros quando nao tens net e assim, o data é onde vem o json ou o erro do body, o response e onde veem os headers.
if (error == nil) { //isto assume que o request foi successfull a nivel de execucao, nao de integridade de dados
extension UIApplication {
class var statusBarBackgroundColor: UIColor? {
get {
return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
} set {
(shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
}
}
}
extension UIApplication {
class var statusBarBackgroundColor: UIColor? {
get {
return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
} set {
(shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
}
}
}
[
{
"account_id": "7ad216c0-df13-42fc-902a-3ef5778029d0",
"created_at": "2017-03-20 15:40:15",
"customer_id": "00000000-0000-0000-0000-000000000001",
"display_id": "61777",
"id": "f1c61484-87b2-4133-96bf-6933d277cf67",
"last_seen_at": "2017-03-24 10:46:32.010368",
"native_id": "61777",
"provider_native_details": {
@flaviodsilverio
flaviodsilverio / DisclosureIndicator.swift
Created December 1, 2016 13:54
Extension to get the default disclosure indicator from tableview cells
extension UIImage {
class func disclosureIndicator() -> UIImage? {
let disclosureCell = UITableViewCell()
disclosureCell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
for view in disclosureCell.subviews {
if let button = view as? UIButton {
@flaviodsilverio
flaviodsilverio / CoreData+Extensions
Last active June 5, 2022 04:05
Useful Extensions for NSManagedObject + NSManagedObjectContext
import CoreData
extension NSManagedObject {
class var entityName: String! {
get {
return self.entity().managedObjectClassName.components(separatedBy: ["."]).last!
}
}
func registerForPushNotifications(application: UIApplication) { // just call this on idFinishLaunchingWith options
let notificationSettings = UIUserNotificationSettings(
types: [.badge, .sound, .alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .none {