(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).
My take-aways are:
You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.
Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse
func countLabelLines(label: UILabel) -> Int { | |
// Call self.layoutIfNeeded() if your view uses auto layout | |
let myText = label.text! as NSString | |
let rect = CGSize(width: label.bounds.width, height: CGFloat.greatestFiniteMagnitude) | |
let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil) | |
return Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight)) | |
} |
+ (void)saveImage:(NSImage *)image atPath:(NSString *)path { | |
CGImageRef cgRef = [image CGImageForProposedRect:NULL | |
context:nil | |
hints:nil]; | |
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef]; | |
[newRep setSize:[image size]]; // if you want the same resolution | |
NSData *pngData = [newRep representationUsingType:NSPNGFileType properties:nil]; | |
[pngData writeToFile:path atomically:YES]; | |
[newRep autorelease]; | |
} |
Edit Feb 4 5:16 PM: Skip to the bottom if you just want the article
It has been brought to my attention that rehosting someone else's content without asking them — even if you link to the original content — is not exactly polite. I did not ask the author before I rehosted his article, and while I feel I should have known better than to that, it just didn't occurr to me. It's not exactly plagarism, but it's still wrong on some level. I have reached out to him now about hosting it here publically, and if he says it's alright, I'll put it back.
You can find the original article here, on his site, and on his Medium page.
import Foundation | |
let config = NSURLSessionConfiguration.defaultSessionConfiguration() | |
let userPasswordString = "username@gmail.com:password" | |
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding) | |
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(nil) | |
let authString = "Basic \(base64EncodedCredential)" | |
config.HTTPAdditionalHeaders = ["Authorization" : authString] | |
let session = NSURLSession(configuration: config) |