Objective-c basics: Object declared in MyAppDelegate not accessible in another class

萝らか妹 提交于 2019-12-09 07:25:13

问题


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.


回答1:


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



回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/5611972/objective-c-basics-object-declared-in-myappdelegate-not-accessible-in-another-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!