我想将响应转换为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]
};

10-04 14:00