How to use background thread in swift?

Swift 3.0+ A lot has been modernized in Swift 3.0. Running something on a background queue looks like this: DispatchQueue.global(qos: .userInitiated).async { print(“This is run on a background queue”) DispatchQueue.main.async { print(“This is run on the main queue, after the previous code in outer block”) } } Swift 1.2 through 2.3 let qualityOfServiceClass = QOS_CLASS_USER_INITIATED … Read more

Can you animate a height change on a UITableViewCell when selected?

I found a REALLY SIMPLE solution to this as a side-effect to a UITableView I was working on….. Store the cell height in a variable that reports the original height normally via the tableView: heightForRowAtIndexPath:, then when you want to animate a height change, simply change the value of the variable and call this… [tableView … Read more

Add an element to an array in Swift

As of Swift 3 / 4 / 5, this is done as follows. To add a new element to the end of an Array. anArray.append(“This String”) To append a different Array to the end of your Array. anArray += [“Moar”, “Strings”] anArray.append(contentsOf: [“Moar”, “Strings”]) To insert a new element into your Array. anArray.insert(“This String”, at: … Read more

Text inset for UITextField?

Overriding -textRectForBounds: will only change the inset of the placeholder text. To change the inset of the editable text, you need to also override -editingRectForBounds: // placeholder position – (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10); } // text position – (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10); }

AVFoundation, how to turn off the shutter sound when captureStillImageAsynchronouslyFromConnection?

I used this code once to capture iOS default shutter sound (here is list of sound file names https://github.com/TUNER88/iOSSystemSoundsLibrary): NSString *path = @”/System/Library/Audio/UISounds/photoShutter.caf”; NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSData *data = [NSData dataWithContentsOfFile:path]; [data writeToFile:[docs stringByAppendingPathComponent:@”photoShutter.caf”] atomically:YES]; Then I used third-party app to extract photoShutter.caf from Documents directory (DiskAid for Mac). Next step … Read more

UIView with rounded corners and drop shadow?

Swift // corner radius blueView.layer.cornerRadius = 10 // border blueView.layer.borderWidth = 1.0 blueView.layer.borderColor = UIColor.black.cgColor // shadow blueView.layer.shadowColor = UIColor.black.cgColor blueView.layer.shadowOffset = CGSize(width: 3, height: 3) blueView.layer.shadowOpacity = 0.7 blueView.layer.shadowRadius = 4.0 Exploring the options Problem 1: Shadow gets clipped off What if there are sublayers or subviews (like an image) whose content we want … Read more

How to print out the method name and line number and conditionally disable NSLog?

Here are some useful macros around NSLog I use a lot: #ifdef DEBUG # define DLog(fmt, …) NSLog((@”%s [Line %d] ” fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #else # define DLog(…) #endif // ALog always displays output regardless of the DEBUG setting #define ALog(fmt, …) NSLog((@”%s [Line %d] ” fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) The DLog macro is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)