Figure out size of UILabel based on String in Swift

I am trying to calculate the height of a UILabel based on different String lengths. func calculateContentHeight() -> CGFloat{ var maxLabelSize: CGSize = CGSizeMake(frame.size.width – 48, CGFloat(9999)) var contentNSString = contentText as NSString var expectedLabelSize = contentNSString.boundingRectWithSize(maxLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16.0)], context: nil) print(“\(expectedLabelSize)”) return expectedLabelSize.size.height } Above is the current function I use … Read more

How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?

I have lots of code in Swift 2.x (or even 1.x) projects that looks like this: // Move to a background thread to do some long running work dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let image = self.loadOrGenerateAnImage() // Bounce back to the main thread to update the UI dispatch_async(dispatch_get_main_queue()) { self.imageView.image = image } } Or stuff … Read more

Command Line Tool – Error – xcrun: error: unable to find utility “xcodebuild”, not a developer tool or in PATH

I am getting this error while building the SwiftJSON framework to the Some Xcode project through Carthage Dependency Manager. Sivaramaiahs-Mac-mini:GZipDemoApp vsoftMacmini5$ carthage update –platform iOS *** Fetching GzipSwift *** Fetching SwiftyJSON *** Checking out GzipSwift at “3.1.1” *** Downloading SwiftyJSON.framework binary at “3.1.3” *** xcodebuild output can be found in /var/folders/7m/y0r2mdhn0f16zz1nlt34ypzr0000gn/T/carthage-xcodebuild.apLXCc.log A shell task (/usr/bin/xcrun … Read more

How to provide a localized description with an Error type in Swift?

I am defining a custom error type with Swift 3 syntax and I want to provide a user-friendly description of the error which is returned by the localizedDescription property of the Error object. How can I do it? public enum MyError: Error { case customError var localizedDescription: String { switch self { case .customError: return … Read more

How to program a delay in Swift 3

In earlier versions of Swift, one could create a delay with the following code: let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { //put your code which should be executed with a delay here } But now, in Swift 3, Xcode automatically changes 6 different things but then the following error appears: “Cannot convert … Read more