Detect if the application in background or foreground in swift

[UIApplication sharedApplication].applicationState will return current state of applications such as: UIApplicationStateActive UIApplicationStateInactive UIApplicationStateBackground or if you want to access via notification see UIApplicationDidBecomeActiveNotification Swift 3+ let state = UIApplication.shared.applicationState if state == .background || state == .inactive { // background } else if state == .active { // foreground } switch UIApplication.shared.applicationState { case .background, … Read more

Convert request Function to Generic type

According to my comment I recommend to use a protocol with extension for example protocol Fetchable { associatedtype FetchableType: NSManagedObject = Self static var entityName : String { get } static var managedObjectContext : NSManagedObjectContext { get } static func objects(for predicate: NSPredicate?) throws -> [FetchableType] } extension Fetchable where Self : NSManagedObject { static … Read more

Convert Swift Array to Dictionary with indexes [duplicate]

try like this: reduce(enumerate(a), [String:UIView]()) { (var dict, enumeration) in dict[“\(enumeration.index)”] = enumeration.element return dict } Xcode 8 • Swift 2.3 extension Array where Element: AnyObject { var indexedDictionary: [String:Element] { var result: [String:Element] = [:] for (index, element) in enumerate() { result[String(index)] = element } return result } } Xcode 8 • Swift 3.0 … Read more

How to open the Document files e.g(.pdf,.doc,.docx) in ios mobile when a button action using swift3.0?

Swift 3*, 4*, To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView, … Read more

Swift 3 iOS compatibility

You can make your app run on iOS 8 & 9 by setting the Deployment Target to one of these versions. Swift 3.x is compatible with iOS 8 and newer (I’m not sure, but it might be also compatible with iOS 7). The only difference to Swift 2.2 (regarding the system requirements) is that you … Read more

Updating closures to Swift 3 – @escaping

Swift 3: closure parameter attributes are now applied to the parameter type, and not the parameter itself Prior to Swift 3, the closure attributes @autoclosure and @noescape used to be attributes to the closure parameter, but are now attributes to the parameter type; see the following accepted Swift evolution proposal: SE-0049: Move @noescape and @autoclosure … Read more