在 Swift Class returns 中访问 Objective C AppDelegate 字符串 nil
Accessing Objective C AppDelegate String in Swift Class returns nil
我正在尝试从我的 ObjectiveC AppDelegate in/using a Swift class 访问 NSNumber 值,但是它在 Swift 上连续 returns nil事情的一面 - 但是它填充在 Objective C class 中(我相信这是由于我在 Swift 中调用它的方式 - 即使它是使用代码完成找到的 - 对于一些原因是它在两者之间经过时仍然会迷路)。
如有任何建议,我们将不胜感激。
Objective C AppDelegate.m:
...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
theNotification = notification;
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
NSNumber *chatId = [aps objectForKey:@"chatId"];
}
...
Objective C AppDelegate.h:
...
@property (nonatomic) NSNumber* chatId;
...
Swift:
class func clientChatSendMessage(_ chatId: NSNumber, message: String, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask {
let oCchatId: AppDelegate = AppDelegate()
let chatSessionId:NSNumber = oCchatId.chatId
在您的代码中创建新变量 NSNumber *chatId = [aps objectForKey:@"chatId"];
试试这个:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
theNotification = notification;
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
self.chatId = [aps objectForKey:@"chatId"];
}
编辑
正如 Caleb 在评论中所说
I think the new instance of AppDelegate is also a problem. It should
be accessed with UIApplication.shared.delegate.chatId
所以你也需要改变这个
class func clientChatSendMessage(_ chatId: NSNumber, message: String, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask {
let oCchatId: AppDelegate = UIApplication.shared.delegate() as! AppDelegate
let chatSessionId:NSNumber = oCchatId.chatId
我正在尝试从我的 ObjectiveC AppDelegate in/using a Swift class 访问 NSNumber 值,但是它在 Swift 上连续 returns nil事情的一面 - 但是它填充在 Objective C class 中(我相信这是由于我在 Swift 中调用它的方式 - 即使它是使用代码完成找到的 - 对于一些原因是它在两者之间经过时仍然会迷路)。
如有任何建议,我们将不胜感激。
Objective C AppDelegate.m:
...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
theNotification = notification;
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
NSNumber *chatId = [aps objectForKey:@"chatId"];
}
...
Objective C AppDelegate.h:
...
@property (nonatomic) NSNumber* chatId;
...
Swift:
class func clientChatSendMessage(_ chatId: NSNumber, message: String, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask {
let oCchatId: AppDelegate = AppDelegate()
let chatSessionId:NSNumber = oCchatId.chatId
在您的代码中创建新变量 NSNumber *chatId = [aps objectForKey:@"chatId"];
试试这个:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
theNotification = notification;
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
self.chatId = [aps objectForKey:@"chatId"];
}
编辑
正如 Caleb 在评论中所说
I think the new instance of AppDelegate is also a problem. It should be accessed with UIApplication.shared.delegate.chatId
所以你也需要改变这个
class func clientChatSendMessage(_ chatId: NSNumber, message: String, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask {
let oCchatId: AppDelegate = UIApplication.shared.delegate() as! AppDelegate
let chatSessionId:NSNumber = oCchatId.chatId