N
N
NONAME82018-08-09 14:17:01
Swift
NONAME8, 2018-08-09 14:17:01

How do nested functions work?

I often use libraries and frameworks, but I don't fully understand how they work.
Question 1:
How can you get a variable from a function, and even an optional one, and then get a variable from it?
I can't imagine how this should be described.
For example this expression:

Auth.auth().currentUser?.providerData[indexPath.row] ( пример из документации Firebase )

Question 2:
What does "?" mean in this example? after method? As far as I know, the method can be optional only in @objc protocoland nowhere else, but here is something like an optional method. How can this be described, and how does it work in general?
Example:
if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
   return true
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2018-08-09
@NONAME8

Good day. The question is a little strange.
It is enough to go here and find out that the method authonly returns an instance of the class. FIRAuthActually, all of yours currentUserare present here. I think now there should be no questions.

spoiler
@implementation FIRAuth {
  /** @var _currentUser
      @brief The current user.
   */
  FIRUser *_currentUser;

  /** @var _firebaseAppName
      @brief The Firebase app name.
   */
  NSString *_firebaseAppName;

  /** @var _listenerHandles
      @brief Handles returned from @c NSNotificationCenter for blocks which are "auth state did
          change" notification listeners.
      @remarks Mutations should occur within a @syncronized(self) context.
   */
  NSMutableArray<FIRAuthStateDidChangeListenerHandle> *_listenerHandles;

  /** @var _keychain
      @brief The keychain service.
   */
  FIRAuthKeychain *_keychain;

  /** @var _lastNotifiedUserToken
      @brief The user access (ID) token used last time for posting auth state changed notification.
   */
  NSString *_lastNotifiedUserToken;

  /** @var _autoRefreshTokens
      @brief This flag denotes whether or not tokens should be automatically refreshed.
      @remarks Will only be set to @YES if the another Firebase service is included (additionally to
        Firebase Auth).
   */
  BOOL _autoRefreshTokens;

  /** @var _autoRefreshScheduled
      @brief Whether or not token auto-refresh is currently scheduled.
   */
  BOOL _autoRefreshScheduled;

  /** @var _isAppInBackground
      @brief A flag that is set to YES if the app is put in the background and no when the app is
          returned to the foreground.
   */
  BOOL _isAppInBackground;

  /** @var _applicationDidBecomeActiveObserver
      @brief An opaque object to act as the observer for UIApplicationDidBecomeActiveNotification.
   */
  id<NSObject> _applicationDidBecomeActiveObserver;

  /** @var _applicationDidBecomeActiveObserver
      @brief An opaque object to act as the observer for
          UIApplicationDidEnterBackgroundNotification.
   */
  id<NSObject> _applicationDidEnterBackgroundObserver;
}

You can imagine it like this:
class Auth {
 class func auth() -> FIRAuth { ... }
 ...
}

class FIRAuth {
  var currentUser: FIRUser?
  ...
}

class FIRUser {
 var providerData: [Int : {что-то}]
 ...
}

Auth.auth().currentUser?.providerData[indexPath.row]

Seems even weirder. Do you know what an optional is? Then have you forgotten that a method can return any value, including an optional one. Now it becomes clear that with any optional we can also apply an optional chain, which is actually what was done here.
FUIAuth.defaultAuthUI()returns an optional. Why not continue the chain. We continue:
FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication)
But oddly enough, this method also returns an optional, and we just want to protect ourselves from this and say that if the last method also has an optional, then return the right side, that is, false. As a result, we get the big picture:
FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question