Objective-c 基础知识:在 MyAppDelegate 中声明的对象无法在另一个类中访问

发布于 2024-10-31 20:17:12 字数 484 浏览 8 评论 0原文

我在我的应用程序委托中声明了一个对象:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-11-07 20:17:12

你的其他类首先应该如何知道这个变量?

您在此处发布的内容显示您的 AppDelegate 类的实例有一个名为 className 的成员,其类型为 ClassName。这意味着在 AppDelegate-class 的每个实例方法(以减号开头的实例方法)中,您可以通过名称 className 访问该变量。

然而,这并不意味着您可以从其他任何地方直接访问此变量!事实上,恰恰相反的情况更接近事实。

如果您想从其他地方访问该变量,有几个选项 - 最常见的一个可能是为其提供一个访问器方法(为此,也有几个选项)。

考虑以下事项:

@interface ClassA : NSObject {
  NSMutableString *interestingMember;
  NSMutableString *inaccessibleMember;
}
-(NSMutableString*)interestingMember;
@end

@interface ClassB : NSObject {
}
-(void)appendString:(NSString*) toMemberOfObject:(ClassA*);
@end
@implementation ClassB
-(void)appendString:(NSString*)string toMemberOfObject:(ClassA*)object
{
  [[object interestingMember] appendString:string]; //this will work: you can access the variable through its accessor
  [inaccessibleMember length]; // this will give a compile error, because the variable is undefined in the current scope
}
@end

因为这是相当基本的面包和面包。关于 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 named className, that is of type ClassName. This means that in every instance-method of the AppDelegate-class (the ones starting with a minus sign) you can access that variable by the name className.

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:

@interface ClassA : NSObject {
  NSMutableString *interestingMember;
  NSMutableString *inaccessibleMember;
}
-(NSMutableString*)interestingMember;
@end

@interface ClassB : NSObject {
}
-(void)appendString:(NSString*) toMemberOfObject:(ClassA*);
@end
@implementation ClassB
-(void)appendString:(NSString*)string toMemberOfObject:(ClassA*)object
{
  [[object interestingMember] appendString:string]; //this will work: you can access the variable through its accessor
  [inaccessibleMember length]; // this will give a compile error, because the variable is undefined in the current scope
}
@end

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.

梦初启 2024-11-07 20:17:12

要从一个类中访问另一个类中的实例变量,您应该创建一个属性。

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    ClassName *className;
}

@property (nonatomic, readonly) ClassName *className;

...
@end

然后,您可以从另一个类访问此属性,如下所示:

@implementation AnotherClass
- (void) doMethod {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.className doClassNameMethod];
}
@end

To access a instance variable in one class from another class, you should create a property.

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    ClassName *className;
}

@property (nonatomic, readonly) ClassName *className;

...
@end

From the other class you may then access this property like this:

@implementation AnotherClass
- (void) doMethod {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.className doClassNameMethod];
}
@end
沉鱼一梦 2024-11-07 20:17:12

className 不在 AnotherClass 的范围内,它没有从 MyAppDelegate 继承任何内容,您可以创建一个 MyAppDelegate > AnotherClass 中的对象并利用 className 变量供您使用,但您仍然需要使用 MyAppDelegate 中的访问器方法来与其通信。

TLDR:#import "MyAppDelegate.h" 只允许您在 AnotherClass 中创建 MyAppDelegate 对象,而不能使用该类中的实例变量。

你想在这两堂课中做什么?

The className is not within the scope of AnotherClass, it does not inherit anything from MyAppDelegate, you can create a MyAppDelegate object in AnotherClass and utilize the className variable for your uses but you still need to use the accessor methods in MyAppDelegate to talk to it.

TLDR: #import "MyAppDelegate.h" only allows you to create a MyAppDelegate object in AnotherClass not use the instance variables within that class.

What are you trying to do exactly within these two classes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文