在Swift Class中访问Objective C AppDelegate String返回nil

时间:2023-01-16 12:05:32

I'm attempting to access a NSNumber value from my ObjectiveC AppDelegate in/using a Swift class however it continuously returns nil on the Swift side of things - however it is populated in the Objective C class (I believe it is due to how I am calling it in Swift - even though it is found using code completion - for some reason it is still getting lost when passing between the two).

我试图从我的ObjectiveC AppDelegate中使用Swift类访问NSNumber值,但是它在Swift方面不断返回nil - 但是它是在Objective C类中填充的(我相信这是由于我是怎样的)在Swift中调用它 - 即使它是使用代码完成找到的 - 由于某种原因它在两者之间传递时仍然会丢失)。

Any suggestions are appreciated.

任何建议表示赞赏。

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

1 个解决方案

#1


1  

In your code you create new variable NSNumber *chatId = [aps objectForKey:@"chatId"];

在您的代码中,您创建新变量NSNumber * chatId = [aps objectForKey:@“chatId”];

Try this:

尝试这个:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
    theNotification = notification;
    NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
    self.chatId = [aps objectForKey:@"chatId"];

}

EDIT

编辑

As Caleb said in comments

正如迦勒在评论中所说

I think the new instance of AppDelegate is also a problem. It should be accessed with UIApplication.shared.delegate.chatId

我认为AppDelegate的新实例也是一个问题。应该使用UIApplication.shared.delegate.chatId访问它

So you need to change this too

所以你也需要改变它

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

#1


1  

In your code you create new variable NSNumber *chatId = [aps objectForKey:@"chatId"];

在您的代码中,您创建新变量NSNumber * chatId = [aps objectForKey:@“chatId”];

Try this:

尝试这个:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
    theNotification = notification;
    NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
    self.chatId = [aps objectForKey:@"chatId"];

}

EDIT

编辑

As Caleb said in comments

正如迦勒在评论中所说

I think the new instance of AppDelegate is also a problem. It should be accessed with UIApplication.shared.delegate.chatId

我认为AppDelegate的新实例也是一个问题。应该使用UIApplication.shared.delegate.chatId访问它

So you need to change this too

所以你也需要改变它

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