Objective-c 基础知识:在 MyAppDelegate 中声明的对象无法在另一个类中访问
我在我的应用程序委托中声明了一个对象:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
ClassName *className;
}
在另一个类中,我包含应用程序委托:
#import "MyAppDelegate.h"
@implementation AnotherClass
-(void)doMethod {
[className doClassNameMethod];
}
由于 className 未声明,因此在编译时失败。既然我已经包含了声明 className 的 MyAppDelegate,那么它不应该是可访问的吗?我需要 AnotherClass.doMethod 来访问在 MyAppDelegate 中创建的同一实例。
对此的任何帮助将不胜感激。谢谢。
I have an object declared in my app delegate:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
ClassName *className;
}
In another class, I include the app delegate:
#import "MyAppDelegate.h"
@implementation AnotherClass
-(void)doMethod {
[className doClassNameMethod];
}
This fails at compile time due to className being undeclared. Shouldn't it be accessible, since I've included the MyAppDelegate, where className is declared? I need AnotherClass.doMethod to be accessing the same instance that is created in MyAppDelegate.
Any help on this would be most appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的其他类首先应该如何知道这个变量?
您在此处发布的内容显示您的
AppDelegate
类的实例有一个名为className
的成员,其类型为ClassName
。这意味着在AppDelegate-class
的每个实例方法(以减号开头的实例方法)中,您可以通过名称className
访问该变量。然而,这并不意味着您可以从其他任何地方直接访问此变量!事实上,恰恰相反的情况更接近事实。
如果您想从其他地方访问该变量,有几个选项 - 最常见的一个可能是为其提供一个访问器方法(为此,也有几个选项)。
考虑以下事项:
因为这是相当基本的面包和面包。关于 OOP 的黄油内容,我鼓励您阅读 学习Objective C:入门以及 Apple 网站上的一些其他介绍性材料。
How should your other class know about this variable in the first place?
What you have posted here, shows that instances of your
AppDelegate
class have a member namedclassName
, that is of typeClassName
. This means that in every instance-method of theAppDelegate-class
(the ones starting with a minus sign) you can access that variable by the nameclassName
.This, however, does not mean you can directly access this variable from anywhere else! In fact, the exact opposite is much closer to the truth.
If you want to access that variable from somewhere else, there are a couple of options — the probably most common one would be to provide an accessor method for it (and for doing this, there are again a couple of options).
Consider the following:
Since this is the fairly basic bread & butter stuff of OOP, I'd encourage you to read Learning Objective C: A Primer and some of the other introductory material on Apple's website.
要从一个类中访问另一个类中的实例变量,您应该创建一个属性。
然后,您可以从另一个类访问此属性,如下所示:
To access a instance variable in one class from another class, you should create a property.
From the other class you may then access this property like this:
className
不在AnotherClass
的范围内,它没有从MyAppDelegate
继承任何内容,您可以创建一个MyAppDelegate
>AnotherClass
中的对象并利用className
变量供您使用,但您仍然需要使用MyAppDelegate
中的访问器方法来与其通信。TLDR:
#import "MyAppDelegate.h"
只允许您在AnotherClass
中创建MyAppDelegate
对象,而不能使用该类中的实例变量。你想在这两堂课中做什么?
The
className
is not within the scope ofAnotherClass
, it does not inherit anything fromMyAppDelegate
, you can create aMyAppDelegate
object inAnotherClass
and utilize theclassName
variable for your uses but you still need to use the accessor methods inMyAppDelegate
to talk to it.TLDR:
#import "MyAppDelegate.h"
only allows you to create aMyAppDelegate
object inAnotherClass
not use the instance variables within that class.What are you trying to do exactly within these two classes?