我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问?

编程入门 行业动态 更新时间:2024-10-21 13:38:50
本文介绍了我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为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.

更多推荐

我如何使其成为类的私有成员或方法,以便可以在静态库本身而不是库外部对其进行访问?

本文发布于:2023-06-07 21:58:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对其   使其   静态   而不是   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!