gpt4 book ai didi

ios - 如何简化此代码(转换为NSDictionary)?

转载 作者:行者123 更新时间:2023-12-01 19:51:17 25 4
gpt4 key购买 nike

我想将响应转换为NSDictionary。响应是ALRegistrationResponse(https://github.com/AppLozic/Applozic-Chat-iOS-Framework/blob/master/Applozic/DomainClasses/ALRegistrationResponse.m)的实现。

NSDictionary *_response = @
{
@"message":[response message] != nil ? [response message] : [NSNull null],
@"deviceKey":[response deviceKey] != nil ? [response deviceKey] : [NSNull null],
@"userKey":[response userKey] != nil ? [response userKey] : [NSNull null],
@"contactNumber":[response contactNumber] != nil ? [response contactNumber] : [NSNull null],
@"lastSyncTime":[response lastSyncTime] != nil ? [response lastSyncTime] : [NSNull null],
@"currentTimeStamp":[response currentTimeStamp] != nil ? [response currentTimeStamp] : [NSNull null],
@"brokerURL":[response brokerURL] != nil ? [response brokerURL] : [NSNull null],
@"imageLink":[response imageLink] != nil ? [response imageLink] : [NSNull null],
@"statusMessage":[response statusMessage] != nil ? [response statusMessage] : [NSNull null],
@"encryptionKey":[response encryptionKey] != nil ? [response encryptionKey] : [NSNull null],
@"displayName":[response displayName] != nil ? [response displayName] : [NSNull null],
@"notificationSoundFileName":[response notificationSoundFileName] != nil ? [response notificationSoundFileName] : [NSNull null]
};

最佳答案

至少更干净的是以下语法:

NSDictionary *_response = @{
@"message": [response message] ?: [NSNull null],
@"deviceKey": [response deviceKey] ?: [NSNull null],
@"userKey": [response userKey] ?: [NSNull null],
@"contactNumber": [response contactNumber] ?: [NSNull null],
@"lastSyncTime": [response lastSyncTime] ?: [NSNull null],
@"currentTimeStamp": [response currentTimeStamp] ?: [NSNull null],
@"brokerURL": [response brokerURL] ?: [NSNull null],
@"imageLink": [response imageLink] ?: [NSNull null],
@"statusMessage": [response statusMessage] ?: [NSNull null],
@"encryptionKey": [response encryptionKey] ?: [NSNull null],
@"displayName": [response displayName] ?: [NSNull null],
@"notificationSoundFileName": [response notificationSoundFileName] ?: [NSNull null]
};

关于ios - 如何简化此代码(转换为NSDictionary)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45755525/

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