From b4b9ae2a42306f86e319b6eddcc1fde8e7409f0e Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Wed, 5 Oct 2016 15:38:31 -0700 Subject: [PATCH] convert sample to swift 3 --- .../swift/uidemo.xcodeproj/project.pbxproj | 112 ++---------------- samples/swift/uidemo/AppDelegate.swift | 8 +- .../Samples/Auth/FIRAuthViewController.swift | 26 ++-- .../Chat/FIRChatCollectionViewCell.swift | 30 ++--- .../Samples/Chat/FIRChatViewController.swift | 90 +++++++------- samples/swift/uidemo/Samples/FIRSample.swift | 18 +-- .../swift/uidemo/Samples/FIRSampleCell.swift | 8 +- .../Samples/FIRSamplesViewController.swift | 18 +-- .../Samples/FIRStorageViewController.swift | 50 ++++---- .../swift/uidemo/UIStoryboardExtension.swift | 4 +- 10 files changed, 138 insertions(+), 226 deletions(-) diff --git a/samples/swift/uidemo.xcodeproj/project.pbxproj b/samples/swift/uidemo.xcodeproj/project.pbxproj index 6504812f37d..262dcbafe94 100644 --- a/samples/swift/uidemo.xcodeproj/project.pbxproj +++ b/samples/swift/uidemo.xcodeproj/project.pbxproj @@ -159,12 +159,9 @@ isa = PBXNativeTarget; buildConfigurationList = 8DABC9A21D3D82D600453807 /* Build configuration list for PBXNativeTarget "uidemo" */; buildPhases = ( - 757DE2C28DE66EF8748CA3DA /* [CP] Check Pods Manifest.lock */, 8DABC9811D3D82D600453807 /* Sources */, 8DABC9821D3D82D600453807 /* Frameworks */, 8DABC9831D3D82D600453807 /* Resources */, - 766A34E36C2BABD9E53324E8 /* [CP] Embed Pods Frameworks */, - 6CC65E746C220141C88C7F20 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -179,12 +176,9 @@ isa = PBXNativeTarget; buildConfigurationList = 8DABC9A51D3D82D600453807 /* Build configuration list for PBXNativeTarget "uidemoTests" */; buildPhases = ( - 235E6CAB9EF32464B5D5C387 /* [CP] Check Pods Manifest.lock */, 8DABC9951D3D82D600453807 /* Sources */, 8DABC9961D3D82D600453807 /* Frameworks */, 8DABC9971D3D82D600453807 /* Resources */, - C6C5C6CC234D3892828C1C9D /* [CP] Embed Pods Frameworks */, - 25A95440B19AD17F8CF61ED7 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -203,14 +197,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = morganchen; TargetAttributes = { 8DABC9841D3D82D600453807 = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0800; }; 8DABC9981D3D82D600453807 = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0800; TestTargetID = 8DABC9841D3D82D600453807; }; }; @@ -255,99 +251,6 @@ }; /* End PBXResourcesBuildPhase section */ -/* Begin PBXShellScriptBuildPhase section */ - 235E6CAB9EF32464B5D5C387 /* [CP] Check Pods Manifest.lock */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Check Pods Manifest.lock"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; - showEnvVarsInLog = 0; - }; - 25A95440B19AD17F8CF61ED7 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-uidemoTests/Pods-uidemoTests-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; - 6CC65E746C220141C88C7F20 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-uidemo/Pods-uidemo-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; - 757DE2C28DE66EF8748CA3DA /* [CP] Check Pods Manifest.lock */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Check Pods Manifest.lock"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; - showEnvVarsInLog = 0; - }; - 766A34E36C2BABD9E53324E8 /* [CP] Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Embed Pods Frameworks"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-uidemo/Pods-uidemo-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; - C6C5C6CC234D3892828C1C9D /* [CP] Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Embed Pods Frameworks"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-uidemoTests/Pods-uidemoTests-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; -/* End PBXShellScriptBuildPhase section */ - /* Begin PBXSourcesBuildPhase section */ 8DABC9811D3D82D600453807 /* Sources */ = { isa = PBXSourcesBuildPhase; @@ -418,8 +321,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -465,8 +370,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -485,6 +392,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -502,6 +410,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -516,6 +425,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.google.firebase.firebaseui.uidemo; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = ""; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -527,6 +437,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.firebase.uidemoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/uidemo.app/uidemo"; }; name = Debug; @@ -539,6 +450,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.firebase.uidemoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/uidemo.app/uidemo"; }; name = Release; diff --git a/samples/swift/uidemo/AppDelegate.swift b/samples/swift/uidemo/AppDelegate.swift index 72e3cff2727..f6c668efe5f 100644 --- a/samples/swift/uidemo/AppDelegate.swift +++ b/samples/swift/uidemo/AppDelegate.swift @@ -25,7 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Successfully running this sample requires an app in Firebase and an // accompanying valid GoogleService-Info.plist file. FIRApp.configure() @@ -33,9 +33,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { - let sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String? - if FIRAuthUI.defaultAuthUI()?.handleOpenURL(url, sourceApplication: sourceApplication) ?? false { + func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool { + let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String? + if FIRAuthUI.default()?.handleOpen(url, sourceApplication: sourceApplication) ?? false { return true } // other URL handling goes here. diff --git a/samples/swift/uidemo/Samples/Auth/FIRAuthViewController.swift b/samples/swift/uidemo/Samples/Auth/FIRAuthViewController.swift index a107000bf32..be53b736678 100644 --- a/samples/swift/uidemo/Samples/Auth/FIRAuthViewController.swift +++ b/samples/swift/uidemo/Samples/Auth/FIRAuthViewController.swift @@ -21,7 +21,7 @@ import FirebaseGoogleAuthUI import FirebaseFacebookAuthUI import FirebaseTwitterAuthUI -let kFirebaseTermsOfService = NSURL(string: "https://firebase.google.com/terms/")! +let kFirebaseTermsOfService = URL(string: "https://firebase.google.com/terms/")! /// A view controller displaying a basic sign-in flow using FIRAuthUI. class FIRAuthViewController: UITableViewController { @@ -30,10 +30,10 @@ class FIRAuthViewController: UITableViewController { // info, see the Auth README at ../../FirebaseAuthUI/README.md // and https://firebase.google.com/docs/auth/ - private var authStateDidChangeHandle: FIRAuthStateDidChangeListenerHandle? + fileprivate var authStateDidChangeHandle: FIRAuthStateDidChangeListenerHandle? - private(set) var auth: FIRAuth? = FIRAuth.auth() - private(set) var authUI: FIRAuthUI? = FIRAuthUI.defaultAuthUI() + fileprivate(set) var auth: FIRAuth? = FIRAuth.auth() + fileprivate(set) var authUI: FIRAuthUI? = FIRAuthUI.default() @IBOutlet weak var cellSignedIn: UITableViewCell! @IBOutlet weak var cellName: UITableViewCell! @@ -45,7 +45,7 @@ class FIRAuthViewController: UITableViewController { @IBOutlet weak var btnAuthorization: UIBarButtonItem! - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tableView.rowHeight = UITableViewAutomaticDimension; @@ -60,24 +60,24 @@ class FIRAuthViewController: UITableViewController { ] self.authUI?.providers = providers - self.authUI?.TOSURL = kFirebaseTermsOfService + self.authUI?.tosurl = kFirebaseTermsOfService self.authStateDidChangeHandle = - self.auth?.addAuthStateDidChangeListener(self.updateUI(auth:user:)) + self.auth?.addStateDidChangeListener(self.updateUI(auth:user:)) } - override func viewWillDisappear(animated: Bool) { + override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) if let handle = self.authStateDidChangeHandle { - self.auth?.removeAuthStateDidChangeListener(handle) + self.auth?.removeStateDidChangeListener(handle) } } - override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } - @IBAction func onAuthorize(sender: AnyObject) { + @IBAction func onAuthorize(_ sender: AnyObject) { if (self.auth?.currentUser) != nil { do { try self.auth?.signOut() @@ -94,12 +94,12 @@ class FIRAuthViewController: UITableViewController { } else { let controller = self.authUI!.authViewController() - self.presentViewController(controller, animated: true, completion: nil) + self.present(controller, animated: true, completion: nil) } } // Boilerplate - func updateUI(auth auth: FIRAuth, user: FIRUser?) { + func updateUI(auth: FIRAuth, user: FIRUser?) { if let user = self.auth?.currentUser { self.cellSignedIn.textLabel?.text = "Signed in" self.cellName.textLabel?.text = user.displayName ?? "(null)" diff --git a/samples/swift/uidemo/Samples/Chat/FIRChatCollectionViewCell.swift b/samples/swift/uidemo/Samples/Chat/FIRChatCollectionViewCell.swift index a0e9c5506a4..65f686f43da 100644 --- a/samples/swift/uidemo/Samples/Chat/FIRChatCollectionViewCell.swift +++ b/samples/swift/uidemo/Samples/Chat/FIRChatCollectionViewCell.swift @@ -19,16 +19,16 @@ import Firebase /// Displays an individual chat message inside of a FIRChatViewController. class FIRChatCollectionViewCell: UICollectionViewCell { - @IBOutlet private(set) var textLabel: UILabel! { + @IBOutlet fileprivate(set) var textLabel: UILabel! { didSet { textLabel.font = FIRChatCollectionViewCell.messageFont } } - static func boundingRectForText(text: String, maxWidth: CGFloat) -> CGRect { + static func boundingRectForText(_ text: String, maxWidth: CGFloat) -> CGRect { let attributes = [NSFontAttributeName: FIRChatCollectionViewCell.messageFont] - let rect = text.boundingRectWithSize(CGSize(width: maxWidth, height: CGFloat.max), - options: [.UsesLineFragmentOrigin], + let rect = text.boundingRect(with: CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude), + options: [.usesLineFragmentOrigin], attributes: attributes, context: nil) return rect @@ -42,14 +42,14 @@ class FIRChatCollectionViewCell: UICollectionViewCell { } /// These constraints are used to left- and right-align chat bubbles. - @IBOutlet private(set) var leadingConstraint: NSLayoutConstraint! - @IBOutlet private(set) var trailingConstraint: NSLayoutConstraint! + @IBOutlet fileprivate(set) var leadingConstraint: NSLayoutConstraint! + @IBOutlet fileprivate(set) var trailingConstraint: NSLayoutConstraint! /// The font used to display chat messages. /// This is the source of truth for the message font, /// overriding whatever is set in interface builder. static var messageFont: UIFont { - return UIFont.systemFontOfSize(UIFont.systemFontSize()) + return UIFont.systemFont(ofSize: UIFont.systemFontSize) } /// Colors for messages (text and background) sent from the client. @@ -57,7 +57,7 @@ class FIRChatCollectionViewCell: UICollectionViewCell { static var selfColors: (background: UIColor, text: UIColor) { return ( background: UIColor(red: 21 / 255, green: 60 / 255, blue: 235 / 255, alpha: 1), - text: UIColor.whiteColor() + text: UIColor.white ) } @@ -66,34 +66,34 @@ class FIRChatCollectionViewCell: UICollectionViewCell { static var othersColors: (background: UIColor, text: UIColor) { return ( background: UIColor(red: 230 / 255, green: 230 / 255, blue: 230 / 255, alpha: 1), - text: UIColor.blackColor() + text: UIColor.black ) } /// Sets the cell's contents and lays out the cell according /// to the contents set. - func populateCellWithChat(chat: Chat, user: FIRUser?, maxWidth: CGFloat) { + func populateCellWithChat(_ chat: Chat, user: FIRUser?, maxWidth: CGFloat) { self.textLabel.text = chat.text let leftRightPadding: CGFloat = 24 let rect = FIRChatCollectionViewCell.boundingRectForText(self.textLabel.text!, maxWidth: maxWidth) - let constant = max(maxWidth - rect.size.width - leftRightPadding, CGFloat.min) + let constant = max(maxWidth - rect.size.width - leftRightPadding, CGFloat.leastNormalMagnitude) if chat.uid == user?.uid ?? "" { let colors = FIRChatCollectionViewCell.selfColors self.containerView.backgroundColor = colors.background self.textLabel.textColor = colors.text - self.trailingConstraint.active = false + self.trailingConstraint.isActive = false self.leadingConstraint.constant = constant - self.leadingConstraint.active = true + self.leadingConstraint.isActive = true } else { let colors = FIRChatCollectionViewCell.othersColors self.containerView.backgroundColor = colors.background self.textLabel.textColor = colors.text - self.leadingConstraint.active = false + self.leadingConstraint.isActive = false self.trailingConstraint.constant = constant - self.trailingConstraint.active = true + self.trailingConstraint.isActive = true } } } diff --git a/samples/swift/uidemo/Samples/Chat/FIRChatViewController.swift b/samples/swift/uidemo/Samples/Chat/FIRChatViewController.swift index 03e17a94f8e..d280cd21c0b 100644 --- a/samples/swift/uidemo/Samples/Chat/FIRChatViewController.swift +++ b/samples/swift/uidemo/Samples/Chat/FIRChatViewController.swift @@ -26,40 +26,40 @@ class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayou // All of the error handling in this controller is done with `fatalError`; // please don't copy paste it into your production code. - private static let reuseIdentifier = "FIRChatCollectionViewCell" + fileprivate static let reuseIdentifier = "FIRChatCollectionViewCell" - @IBOutlet private var collectionView: UICollectionView! - @IBOutlet private var textView: UITextView! { + @IBOutlet fileprivate var collectionView: UICollectionView! + @IBOutlet fileprivate var textView: UITextView! { didSet { - textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor + textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor textView.layer.borderWidth = 1 textView.layer.cornerRadius = 8 textView.layer.masksToBounds = true } } - @IBOutlet private var sendButton: UIButton! + @IBOutlet fileprivate var sendButton: UIButton! /// Used to shift view contents up when the keyboard appears. - @IBOutlet private var bottomConstraint: NSLayoutConstraint! + @IBOutlet fileprivate var bottomConstraint: NSLayoutConstraint! - private let auth = FIRAuth.auth() - private let chatReference = FIRDatabase.database().reference().child("swift_demo-chat") + fileprivate let auth = FIRAuth.auth() + fileprivate let chatReference = FIRDatabase.database().reference().child("swift_demo-chat") - private var collectionViewDataSource: FirebaseCollectionViewDataSource! + fileprivate var collectionViewDataSource: FirebaseCollectionViewDataSource! - private var user: FIRUser? - private var query: FIRDatabaseQuery? + fileprivate var user: FIRUser? + fileprivate var query: FIRDatabaseQuery? - private var authStateListenerHandle: FIRAuthStateDidChangeListenerHandle? + fileprivate var authStateListenerHandle: FIRAuthStateDidChangeListenerHandle? // MARK: - Interesting stuff - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - self.authStateListenerHandle = self.auth?.addAuthStateDidChangeListener { (auth, user) in + self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in self.user = user - self.query = self.chatReference.queryLimitedToLast(50) + self.query = self.chatReference.queryLimited(toLast: 50) // The initializer called below--though it takes a collection view-- // doesn't actually set the collection view's data source, so if @@ -69,7 +69,7 @@ class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayou view: self.collectionView) self.collectionView.dataSource = self.collectionViewDataSource - self.collectionViewDataSource.populateCellWithBlock { (anyCell, data) in + self.collectionViewDataSource.populateCell { (anyCell, data) in guard let cell = anyCell as? FIRChatCollectionViewCell else { fatalError("Unexpected collection view cell class \(anyCell.self)") } @@ -82,12 +82,12 @@ class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayou // but unfortunately FirebaseCollectionViewDataSource uses the FirebaseArray // delegate methods to update its own internal state, so in order to scroll // on new insertions we still need to use the query directly. - self.query!.observeEventType(.ChildAdded, withBlock: { [unowned self] _ in + self.query!.observe(.childAdded, with: { [unowned self] _ in self.scrollToBottom(animated: true) }) } - self.auth?.signInAnonymouslyWithCompletion { (user, error) in + self.auth?.signInAnonymously { (user, error) in if let error = error { // An error here means the user couldn't sign in. Correctly // handling it depends on the context as well as your app's @@ -98,20 +98,20 @@ class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayou } // Notification boilerplate to handle keyboard appearance/disappearance - NSNotificationCenter.defaultCenter().addObserver(self, + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), - name: UIKeyboardWillShowNotification, + name: NSNotification.Name.UIKeyboardWillShow, object: nil) - NSNotificationCenter.defaultCenter().addObserver(self, + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), - name: UIKeyboardWillHideNotification, + name: NSNotification.Name.UIKeyboardWillHide, object: nil) } - @objc private func didTapSend(sender: AnyObject) { + @objc fileprivate func didTapSend(_ sender: AnyObject) { guard let user = self.auth?.currentUser else { return } let uid = user.uid - let name = "User " + uid[uid.characters.startIndex.. CGSize { + func collectionView(_ collectionView: UICollectionView, layout + collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let heightPadding: CGFloat = 16 let width = self.view.frame.size.width - let blob = self.collectionViewDataSource.objectAtIndex(UInt(indexPath.row)) as! FIRDataSnapshot + let blob = self.collectionViewDataSource.object(at: UInt((indexPath as NSIndexPath).row)) as! FIRDataSnapshot let text = Chat(snapshot: blob)!.text let rect = FIRChatCollectionViewCell.boundingRectForText(text, maxWidth: width) diff --git a/samples/swift/uidemo/Samples/FIRSample.swift b/samples/swift/uidemo/Samples/FIRSample.swift index 843f3312224..d643375889e 100644 --- a/samples/swift/uidemo/Samples/FIRSample.swift +++ b/samples/swift/uidemo/Samples/FIRSample.swift @@ -23,9 +23,9 @@ enum FIRSample: Int, RawRepresentable { // When adding new samples, add a new value here and fill // out the switch statements below as necessary. - case Auth = 0 - case Chat = 1 - case Storage = 2 + case auth = 0 + case chat = 1 + case storage = 2 static var total: Int { var count = 0 @@ -37,17 +37,17 @@ enum FIRSample: Int, RawRepresentable { var labels: (title: String, subtitle: String) { switch self { - case .Chat: + case .chat: return ( title: "Chat", subtitle: "Demonstrates using a FirebaseCollectionViewDataSource to load data from Firebase Database into a UICollectionView for a basic chat app." ) - case .Auth: + case .auth: return ( title: "Auth", subtitle: "Demonstrates the FirebaseAuthUI flow with customization options." ) - case .Storage: + case .storage: return ( title: "Storage", subtitle: "Demonstrates using FirebaseStorageUI to populate an image view." @@ -57,11 +57,11 @@ enum FIRSample: Int, RawRepresentable { func controller() -> UIViewController { switch self { - case .Chat: + case .chat: return UIStoryboard.instantiateViewController("Main", identifier: "FIRChatViewController") - case .Auth: + case .auth: return UIStoryboard.instantiateViewController("Main", identifier: "FIRAuthViewController") - case .Storage: + case .storage: return UIStoryboard.instantiateViewController("Main", identifier: "FIRStorageViewController") } } diff --git a/samples/swift/uidemo/Samples/FIRSampleCell.swift b/samples/swift/uidemo/Samples/FIRSampleCell.swift index 8605b15cf03..4cbb80cc78e 100644 --- a/samples/swift/uidemo/Samples/FIRSampleCell.swift +++ b/samples/swift/uidemo/Samples/FIRSampleCell.swift @@ -18,15 +18,15 @@ import UIKit class FIRSampleCell: UITableViewCell { - @IBOutlet private var titleLabel: UILabel! - @IBOutlet private var subtitleLabel: UILabel! + @IBOutlet fileprivate var titleLabel: UILabel! + @IBOutlet fileprivate var subtitleLabel: UILabel! override convenience init(style: UITableViewCellStyle, reuseIdentifier: String?) { self.init(reuseIdentifier: reuseIdentifier!) } init(reuseIdentifier: String) { - super.init(style: .Default, reuseIdentifier: reuseIdentifier) + super.init(style: .default, reuseIdentifier: reuseIdentifier) } convenience init(type: FIRSample, reuseIdentifier: String) { @@ -35,7 +35,7 @@ class FIRSampleCell: UITableViewCell { self.configureWithType(type) } - func configureWithType(type: FIRSample) { + func configureWithType(_ type: FIRSample) { let labels = type.labels self.titleLabel.text = labels.title self.subtitleLabel.text = labels.subtitle diff --git a/samples/swift/uidemo/Samples/FIRSamplesViewController.swift b/samples/swift/uidemo/Samples/FIRSamplesViewController.swift index d7117c95d18..be381fa5d46 100644 --- a/samples/swift/uidemo/Samples/FIRSamplesViewController.swift +++ b/samples/swift/uidemo/Samples/FIRSamplesViewController.swift @@ -21,9 +21,9 @@ import UIKit // in here it's probably a typo or some other small accident. class FIRSamplesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { - private let reuseIdentifier = "FIRSamplesViewControllerCell" + fileprivate let reuseIdentifier = "FIRSamplesViewControllerCell" - @IBOutlet private var tableView: UITableView! + @IBOutlet fileprivate var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() @@ -37,27 +37,27 @@ class FIRSamplesViewController: UIViewController, UITableViewDelegate, UITableVi // MARK: - UITableView Delegate - func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let navController = self.navigationController! // assert nonnull - let targetController = FIRSample(rawValue: indexPath.row)!.controller() + let targetController = FIRSample(rawValue: (indexPath as NSIndexPath).row)!.controller() navController.pushViewController(targetController, animated: true) - tableView.deselectRowAtIndexPath(indexPath, animated: true) + tableView.deselectRow(at: indexPath, animated: true) } // MARK: - UITableView Data Source - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let sampleType = FIRSample(rawValue: indexPath.row)! + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let sampleType = FIRSample(rawValue: (indexPath as NSIndexPath).row)! - let cell = self.tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! FIRSampleCell + let cell = self.tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as! FIRSampleCell cell.configureWithType(sampleType) return cell } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return FIRSample.total } } diff --git a/samples/swift/uidemo/Samples/FIRStorageViewController.swift b/samples/swift/uidemo/Samples/FIRStorageViewController.swift index 0cad2eeac82..accf5ce7575 100644 --- a/samples/swift/uidemo/Samples/FIRStorageViewController.swift +++ b/samples/swift/uidemo/Samples/FIRStorageViewController.swift @@ -20,36 +20,36 @@ import FirebaseStorageUI class FIRStorageViewController: UIViewController { - @IBOutlet private var imageView: UIImageView! - @IBOutlet private var textField: UITextField! + @IBOutlet fileprivate var imageView: UIImageView! + @IBOutlet fileprivate var textField: UITextField! - private var storageRef = FIRStorage.storage().reference() + fileprivate var storageRef = FIRStorage.storage().reference() - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - self.textField.autocorrectionType = .No - self.textField.autocapitalizationType = .None - self.imageView.contentMode = .ScaleAspectFit + self.textField.autocorrectionType = .no + self.textField.autocapitalizationType = .none + self.imageView.contentMode = .scaleAspectFit // Notification boilerplate to handle keyboard appearance/disappearance - NSNotificationCenter.defaultCenter().addObserver(self, + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), - name: UIKeyboardWillShowNotification, + name: NSNotification.Name.UIKeyboardWillShow, object: nil) - NSNotificationCenter.defaultCenter().addObserver(self, + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), - name: UIKeyboardWillHideNotification, + name: NSNotification.Name.UIKeyboardWillHide, object: nil) } - @IBAction private func loadButtonPressed(sender: AnyObject) { + @IBAction fileprivate func loadButtonPressed(_ sender: AnyObject) { self.imageView.image = nil guard let text = self.textField.text else { return } - guard let url = NSURL(string: text) else { return } + guard let url = URL(string: text) else { return } - self.storageRef = FIRStorage.storage().referenceWithPath(url.path ?? "") + self.storageRef = FIRStorage.storage().reference(withPath: url.path) - self.imageView.sd_setImageWithStorageReference(self.storageRef, + self.imageView.sd_setImage(with: self.storageRef, placeholderImage: nil) { (image, error, cacheType, storageRef) in if let error = error { print("Error loading image: \(error)") @@ -60,12 +60,12 @@ class FIRStorageViewController: UIViewController { // MARK: Keyboard boilerplate /// Used to shift textfield up when the keyboard appears. - @IBOutlet private var bottomConstraint: NSLayoutConstraint! + @IBOutlet fileprivate var bottomConstraint: NSLayoutConstraint! - @objc private func keyboardWillShow(notification: NSNotification) { - let userInfo = notification.userInfo! + @objc fileprivate func keyboardWillShow(_ notification: Notification) { + let userInfo = (notification as NSNotification).userInfo! let endFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue - let endHeight = endFrameValue.CGRectValue().size.height + let endHeight = endFrameValue.cgRectValue.size.height self.bottomConstraint.constant = endHeight @@ -73,21 +73,21 @@ class FIRStorageViewController: UIViewController { let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double UIView.setAnimationCurve(curve) - UIView.animateWithDuration(duration) { + UIView.animate(withDuration: duration, animations: { self.view.layoutIfNeeded() - } + }) } - @objc private func keyboardWillHide(notification: NSNotification) { + @objc fileprivate func keyboardWillHide(_ notification: Notification) { self.bottomConstraint.constant = 0 - let userInfo = notification.userInfo! + let userInfo = (notification as NSNotification).userInfo! let curve = UIViewAnimationCurve(rawValue: userInfo[UIKeyboardAnimationCurveUserInfoKey] as! Int)! let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double UIView.setAnimationCurve(curve) - UIView.animateWithDuration(duration) { + UIView.animate(withDuration: duration, animations: { self.view.layoutIfNeeded() - } + }) } } diff --git a/samples/swift/uidemo/UIStoryboardExtension.swift b/samples/swift/uidemo/UIStoryboardExtension.swift index 6952cc2e539..5acc32ff023 100644 --- a/samples/swift/uidemo/UIStoryboardExtension.swift +++ b/samples/swift/uidemo/UIStoryboardExtension.swift @@ -17,7 +17,7 @@ import UIKit extension UIStoryboard { - static func instantiateViewController(storyboard: String, identifier: String) -> UIViewController { - return UIStoryboard(name: storyboard, bundle: nil).instantiateViewControllerWithIdentifier(identifier) + static func instantiateViewController(_ storyboard: String, identifier: String) -> UIViewController { + return UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: identifier) } }