我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问? [英] How can i make it private members or methods of class so that it can be accessed within the static library itself and not outside the library?

查看:55
本文介绍了我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Objective-c中的以下类准备小型静态库:A类,B类,C类.我想将这些类包括在静态库中.现在,A类可以访问B类或C类方法的公共成员.

I want to prepare small static library for below classes in objective-c : Class A, Class B, Class C. I want to include these classes in static library. Now Class A can access public members of methods of Class B or Class C.

现在,当我将上述库集成到其他项目中时,我准备了只能访问A类和B类的D类不是C类.我该怎么办?

Now When I integrate above library in other project, I prepare Class D which can access only Class A and Class B Not Class C. How can I do this ?

我的另一个疑问是假设NSString * isValid在类B中声明.

My other doubt is assume that NSString *isValid is declared in Class B.

我希望可以从A类和C类访问上述变量我的意思是库中包含的文件可以访问上述变量.

I want that above variable can be accessed from Class A and Class C I mean included files of library can access above variable.

但是从外部库上方无法访问变量.如何使其私有化,以便可以在库本身内部而不是库外部对其进行访问?

But from outside library above variable can't be accessed. How can make it private so that it can be accessed within the library itself and not outside the library ?

感谢帮助!

推荐答案

您可以使公共方法仅对静态库可见,而在静态库之外不可见.

You can make public methods only visible to your static library but invisible out side of it.

这是方法.

1)创建要在库外部使用的头文件

1) Create a header file to be used outside of your library

#import <Foundation/Foundation.h>

@interface ClassA : NSObject 

@property(nonatomic,readwrite)BOOL publicProperty;

-(void)publicMethod;

@end

2)创建一个仅在静态库内部使用的类别

2) Create a category to be only used internally by the static library

#import "ClassA.h"

@interface ClassA (Internal)

@property(nonatomic,readwrite)BOOL privateProperty;

-(void)privateMethod;

@end

注意将此文件命名为:"ClassA + Internal.h"

Note Name this file: "ClassA+Internal.h"

3)在.m文件中再次声明您的私有属性和方法

3) Declare your private properties and methods again in the .m file

#import "ClassA.h"

@interface ClassA ()

@property(nonatomic,readwrite)BOOL privateProperty;

-(void)privateMethod;

@end

@implementation ClassA

@synthesize publicProperty;
@synthesize privateProperty;

//...

@end

在静态库中使用私有属性和方法

在您的ClassB.m文件中,导入ClassA类别的头文件

In your ClassB.m file import header file of the ClassA category

#import "ClassB.h"
#import "ClassA.h"
#import "ClassA+Internal.h"

现在您可以访问ClassA的私有属性和方法

Now you have access to the private properties and methods of ClassA

创建没有私有属性和方法的静态库

创建静态库时,将"ClassA + Internal.h"类别头文件保留在构建阶段",复制头"的私有"或项目"头部分中

When you create you static library keep the "ClassA+Internal.h" category header file inside "private" or "project" headers section of "Build Phases","Copy Headers"

这样,当您构建静态库时,外部将无法访问ClassA + Internal.h类别.

This way when you build your static library the ClassA+Internal.h category will be inaccessible to the outside.

这篇关于我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆