How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

Swift 2.0 Pass info using userInfo which is a optional Dictionary of type [NSObject : AnyObject]? let imageDataDict:[String: UIImage] = [“image”: image] // Post a notification NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict) // Register to receive notification in your class NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil) // handle notification func showSpinningWheel(notification: NSNotification) { if let … Read more

Swift 3 URLSession.shared() Ambiguous reference to member ‘dataTask(with:completionHandler:) error (bug)

The compiler is confused by the function signature. You can fix it like this: let task = URLSession.shared.dataTask(with: request as URLRequest) { But, note that we don’t have to cast “request” as URLRequest in this signature if it was declared earlier as URLRequest instead of NSMutableURLRequest: var request = URLRequest(url:myUrl!) This is the automatic casting … Read more

Swift 3.0 Result of call is unused [duplicate]

You are getting this issue because the function you are calling returns a value but you are ignoring the result. There are two ways to solve this issue: Ignore the result by adding _ = in front of the function call Add @discardableResult to the declaration of the function to silence the compiler

try, try! & try? what’s the difference, and when to use each?

Updated for Swift 5.1 Assume the following throwing function: enum ThrowableError: Error { case badError(howBad: Int) } func doSomething(everythingIsFine: Bool = false) throws -> String { if everythingIsFine { return “Everything is ok” } else { throw ThrowableError.badError(howBad: 4) } } try You have 2 options when you try calling a function that may throw. … Read more

Figure out size of UILabel based on String in Swift

Use an extension on String Swift 3 extension String { func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat { let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude) let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) return ceil(boundingBox.height) } func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat { let constraintRect = CGSize(width: .greatestFiniteMagnitude, … Read more

CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift

CGRect Can be simply created using an instance of a CGPoint or CGSize, thats given below. let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 100, height: 100)) // Or let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 100)) Or if we want to specify each value in CGFloat or Double or Int, we … Read more

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

Since the beginning, Swift has provided some facilities for making ObjC and C more Swifty, adding more with each version. Now, in Swift 3, the new “import as member” feature lets frameworks with certain styles of C API — where you have a data type that works sort of like a class, and a bunch … Read more

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

I solved that problem by setting the Command Line Tools in Xcode. Go to: Xcode > Preferences > Locations And select the command line tool from the dropdown. If you have only one version of Xcode installed, there should be only one option. If you have several versions of Xcode, then you must choose the … Read more

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

As described in the Xcode 8 beta 6 release notes, Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol. In your case: public enum MyError: Error { case customError } extension MyError: LocalizedError { public var errorDescription: String? { switch self { case .customError: return NSLocalizedString(“A user-friendly description of the … Read more