class extension in objective-c

前端 未结 6 1418
花落未央
花落未央 2021-01-07 01:48

I am learning object-c by reading a book. When I read the chapter about class extension, the book gives the following example code:

// A class extension
@int         


        
6条回答
  •  滥情空心
    2021-01-07 02:31

    Think contexts, any other class can access the properties declared at the extensions or any categories as far as the extended interfaces are "visible" at the given context.

    For example, the following implementation file contains implementation of two interfaces: BaseObject and BaseObjectController. At the implementation of the other class (BaseObjectController) you can safely use the "hidden" property via getters and setters because the declaration interface is "visible". If you move the implementation of BaseObjectController to another file that can't see declaration of the extension - this code won't compile.

    #import "BaseObject.h"
    #import "BaseObjectController.h"
    
    //  BaseObject    
    @interface BaseObject()
    @property (strong) NSString * idString;
    @end
    
    @implementation BaseObject
    @end
    
    //  BaseObjectController    
    @implementation BaseObjectController
    - (void) initBaseObject {
        BaseObject * bo = [BaseObject new];
        bo.idString = @"01234";
    }
    @end
    

提交回复
热议问题