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

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

didReceiveRemoteNotification not called, iOS 10

type converson for Swift3 – for sample see this import the UserNotifications framework and add the UNUserNotificationCenterDelegate in Appdelegate import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. //create the notificationCenter let center = UNUserNotificationCenter.current() center.delegate = self … Read more

How to convert Data to hex string in swift

A simple implementation (taken from How to hash NSString with SHA1 in Swift?, with an additional option for uppercase output) would be extension Data { struct HexEncodingOptions: OptionSet { let rawValue: Int static let upperCase = HexEncodingOptions(rawValue: 1 << 0) } func hexEncodedString(options: HexEncodingOptions = []) -> String { let format = options.contains(.upperCase) ? “%02hhX” … Read more

Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

As per SE-0054, ImplicitlyUnwrappedOptional<T> is no longer a distinct type; there is only Optional<T> now. Declarations are still allowed to be annotated as implicitly unwrapped optionals T!, but doing so just adds a hidden attribute to inform the compiler that their value may be force unwrapped in contexts that demand their unwrapped type T; their … Read more

DateFormatter doesn’t return date for “HH:mm:ss”

From Technical Q&A QA1480 – NSDateFormatter and Internet Dates (emphasis added): On the other hand, if you’re working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is “en_US_POSIX”, a locale that’s specifically designed to yield US … Read more

Correctly Parsing JSON in Swift 3

First of all never load data synchronously from a remote URL, use always asynchronous methods like URLSession. ‘Any’ has no subscript members occurs because the compiler has no idea of what type the intermediate objects are (for example currently in [“currently”]![“temperature”]) and since you are using Foundation collection types like NSDictionary the compiler has no … Read more

Creation of JSON array in swift 3

Using SwiftyJson var jsonArray: JSON = [“parameter”: [“key”: “paymentType”,”value”: “F”]] Best I could do with the information you gave. Hardcoding values into something like this isn’t usualy the best idea so I would recommend you go and try learn a bit more about SwiftyJSON which might make working with JSON a bit easier for you.