防止从框架中实例化只读对象(Prevent instantiation of readonly objects from framework)

编程入门 行业动态 更新时间:2024-10-27 16:31:13
防止从框架中实例化只读对象(Prevent instantiation of readonly objects from framework)

我目前正在为iOS创建一个客观的c框架,以帮助促进API和开发人员之间的交互。 作为其中的一部分,我返回各种只读对象数组,开发人员可以使用这些对象向用户显示信息。 但是,我想确保向用户显示的对象仅来自框架,并且开发人员无法使用框架进行实例化。

我当前的实现使用一个自定义构造函数初始化程序,它从api中获取JSON来实例化它自己。 我知道访问我的自定义构造函数初始化程序的唯一方法是将其定义放在头文件中,这使得它不仅可以自己访问,还可以访问开发人员。 我知道当用户尝试使用默认构造函数初始化程序时,我可以抛出一个不一致异常, -(id)init; ,但我不能阻止他们创建自己的JSON字符串并调用我的自定义构造函数初始化程序。

我是否采取了正确的方法来保护我的私有框架免受开发人员使用它的干扰? 我还能如何解决这个问题以确保这些对象中数据的有效性?

来源: 是否可以在Objective-C中将-init方法设为私有?

I am currently in the process of creating an objective c framework for iOS to help facilitate the interaction between an API and a developer. As part of this, I return various arrays of readonly objects that a developer can use to display information to the user. However, I would like to ensure that the objects displayed to the user come only from the framework and can not be instantiated by the developer using the framework.

My current implementation uses a custom constructor initializer that takes JSON from the api to instantiate itself. The only way that I am aware of accessing my custom constructor initializer is by putting its definition in the header file which makes it not only accessible to myself, but also the developer. I am aware that I can throw an inconsistency exception when the user tries to use the default constructor initializer, -(id)init;, but I can not stop them from creating their own JSON string and calling my custom constructor initializer.

Am I taking the correct approach to securing my private framework from interference from the developer using it? How else can I get around this to ensure the validity of data in these objects?

Source: Is it possible to make the -init method private in Objective-C?

最满意答案

你是正确的,由于它的动态调度系统,Objective-C不允许真正的私有方法。 但是,假设您的问题不是真正的安全性,而是简单地以不正确的方式使用框架,那么您有一些选择。

一个简单,通用的解决方案是将您不希望公开的方法的声明放在单独的头文件中的类别中。 您仍然可以将这些方法的实现放在类的主实现文件中。 所以,标题包含这样的内容:

// MyClass+Private.h @interface MyClass (Private) - (void)aPrivateMethod; @end

然后,在您自己的源文件中,您需要访问这些私有方法,只需导入MyClass + Private.h。

对于框架,您可以将每个头文件设置为Public或Private。 私有标头不会被复制到框架包中,因此框架的用户将无法看到它们。 您可以通过打开Xcode中的“实用工具”窗格(右侧滑出窗格),选择相关标题,然后在“目标成员资格”下的相关行的第二列中选择“私有”来执行此操作。

You are correct that Objective-C doesn't allow for truly private methods by it's very nature, due to its dynamic dispatch system. However, assuming your question is not about true security, rather simply making it difficult to use the framework in an incorrect way, you have a few options.

A simple, common solution would be to put the declarations for methods you don't want to expose publicly in a category in a separate header file. You can still put these methods' implementations in the main implementation file for the class. So, a header with something like this:

// MyClass+Private.h @interface MyClass (Private) - (void)aPrivateMethod; @end

Then, in your own source files where you need to access those private methods, you simply import MyClass+Private.h.

For a framework, you can set each header file to be Public or Private. Private headers will not be copied into the framework bundle, and therefore won't be visible to users of the Framework. You do this by opening the Utilities pane in Xcode (the right-side slide out pane), selecting the header in question, then choosing Private in the second column of the relevant row under "Target Membership".

更多推荐

本文发布于:2023-08-07 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   实例   对象   Prevent   framework

发布评论

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

>www.elefans.com

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