gpt4 book ai didi

ios - 无法从 AppDelegate 调用变量

转载 作者:可可西里 更新时间:2023-11-01 06:24:33 26 4
gpt4 key购买 nike

我的 AppDelegate.m 中有一个名为 message 的变量,我想在 View Controller 中使用它,但它不起作用。我试过这个解决方案:

  • 如果我将 AppDelegate.m 导入我的 ViewController.m,我会得到一个错误:clang: error: linker command failed with exit code 1 (使用 -v 查看调用),但如果我不导入它,我会得到这个:No known class method for selector 'message' at this line: self.toSort = [AppDelegate 消息];.但是当我将 ViewController.m 导入 AppDelegate.m 时,我没有收到链接器命令错误,但是其他错误已经存在。

我的AppDelegate.h

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) PNChannel *myChannel;
- (void)getMessage;


AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.m"

static NSArray *_message = nil;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// [self.window makeKeyAndVisible];
self.myChannel = [PNChannel channelWithName:currentChannel.username
shouldObservePresence:YES];

[self getMessage];

}
+ (NSArray *)message
{
if (_message)
return _message;

AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate getMessage];

return nil;
}

- (void)getMessage {

[PubNub requestFullHistoryForChannel:self.myChannel withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {

_message = contentArray;
NSLog(@"test log %@", _message);
}];
}

ViewController.m

#import "ViewController.h"

//#import "AppDelegate.h"
//#import "AppDelegate.m"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)viewWillAppear:(BOOL)animated {

//AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//[appDelegate getMessage];

self.toSort = [AppDelegate message];
[self getMessageList];

}

我确定我犯了一些初学者错误,但我想不通。 “测试日志”有效,所以我想我必须以不同的方式调用它。

已经试过了,但是还是报错,因为 message 不是一个属性。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *variableTest = appDelegate.message;
NSLog(@"TEST : %@",variableTest);

更新:我试过了,但是测试日志显示为空,所以还是有问题。

AppDelegate.h

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) PNChannel *myChannel;
@property (strong, nonatomic) NSArray *message;
- (void)getMessage;

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// [self.window makeKeyAndVisible];
self.myChannel = [PNChannel channelWithName:currentChannel.username
shouldObservePresence:YES];

[self getMessage];

}
return YES;
}

+ (NSArray *)message
{
if (self.message)
return self.message;

AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate getMessage];

return nil;
}

- (void)getMessage {

[PubNub requestFullHistoryForChannel:self.myChannel withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {

self.message = contentArray;

NSLog(@"dev log %@", self.message);
}];
}

ViewController.m

- (void)viewWillAppear:(BOOL)animated {


AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *variableTest = appDelegate.message;
NSLog(@"TEST : %@",variableTest);


}

我的尝试基于 o Pi 的回答:

@interface MessageHistoryData : NSObject {
NSArray *yourData;
}
@property(nonatomic,retain) NSArray *yourData;
+(MessageHistoryData *)getInstance;

@end

#import "MessageHistoryData.h"

@implementation MessageHistoryData @synthesize yourData;
static MessageHistoryData *instance =nil;
+(MessageHistoryData *)getInstance {
@synchronized(self) {
if(instance==nil) {
instance= [MessageHistoryData new];
}
}
return instance;
}

@end

在我的ViewController.m中(MessageHistoryData导入到.h中)

-(void)setupArray{

[PubNub requestHistoryForChannel:my_channel from:nil to:nil limit:100 reverseHistory:NO withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {

MessageHistoryData *data = [MessageHistoryData getInstance];

data.yourData = contentArray;

NSLog(@"Dev log2 %@", data.yourData);


}];

最佳答案

我设置了一个示例项目来验证它是否有效。

在AppDelegate.h文件中,公开声明message属性和-getMessage方法:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (readonly, nonatomic) NSString *message;

- (void)getMessage;

@end

在 AppDelegate.m 文件中,像往常一样实现您的方法(为了举例,我明确设置了该属性):

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}

- (void)getMessage {
self.message = @"This is a message";
}

@end

在您的 ViewController.m 文件中,您应该导入 AppDelegate header ,并且您应该可以自由访问属性:

#import "AppDelegate.h"
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(@"The delegate's message is: %@", delegate.message); // Logs "The delegate's message is: (null)"
[delegate getMessage];
NSLog(@"The delegate's message is: %@", delegate.message); // Logs "The delegate's message is: This is a message"
}

@end

如果上述方法不起作用,您应该测试您的 PubNub 类并确保它的行为是可预测的。

我不建议永远在您的 AppDelegate 中存储信息,因为这会使该类负责做更多的事情,而不仅仅是作为您的应用程序对系统的委托(delegate)。此类信息应存储在专用存储中,或通过自定义 PubNub 子类提供,该子类作为单例访问(如果没有要管理的全局状态!)或逐个实例访问。

如果您需要任何说明或者上述解决方案是否不适合您,请告诉我。

编辑:单例建议

根据我的评论,这是处理跨 View Controller 共享网络数据的一种方法

@interface NetworkClient : PubNub

@property (readonly, nonatomic) NSString *message;

/**
* Returns a shared network client to be used throughout the app
*/
+ (instancetype)sharedClient;

- (void)configureWithChannel:(PNChannel*)channel;
- (void)clearChannel;

- (void)getMessagesWithCompletionHandler:(void (^)(NSArray *, PNChannel *, PNDate *, PNDate *, PNError *))

@end

sharedInstance 使用 technique described here 的地方设置您的实例。从那里,您可以使用 [NetworkClient sharedClient] 访问客户端,并通过客户端上的实例方法或属性检索任何数据。

我还猜测您通常是单例或 iOS 的新手,所以我建议您阅读 this article关于使用单例和博客 objc.io熟悉一些绝对会让您的生活更轻松的最佳实践。

关于ios - 无法从 AppDelegate 调用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com