Use of extern in Objective C

后端 未结 5 1671
无人及你
无人及你 2020-12-07 11:30

How good is it to use extern in Objective C? It does make coding for some parts easy.. but doesn\'t it spoil the object orientation?

5条回答
  •  鱼传尺愫
    2020-12-07 11:59

    Depends on your need, for example you have login page. After you logged in you are notifying to other pages in the applications.

    #import 
    
    extern NSString *const DidLoginNotification;
    
    @interface LoginViewController : NSObject
    - (void)login;
    @end
    
    
    // LoginViewController.m
    #import "LoginViewController.h"
    
    //define extern const in implementation file only once for the whole process
    NSString *const DidLoginNotification =
        @"DidLoginNotificationNotified";
    
    @implementation LoginViewController
    
    - (void)login {
        // Perform notification
        [[NSNotificationCenter defaultCenter];
        sendNotificationName: DidLoginNotification
                        object:nil];
    }
    

    The notification receiving party does not need to know the value of the const.

提交回复
热议问题