我不是iOS开发人员,而是选择目标c的rn开发人员。
我有,一个像这样的NSdictionry
NSDictionary *selfInfo = @{
@"id": selfPartcipant.id,
@"name": selfPartcipant.name,
@"picture": selfPartcipant.picture,
@"audioEnabled": @(selfPartcipant.audioEnabled),
@"videoEnabled": @(selfPartcipant.videoEnabled),
@"isPinned": @(selfPartcipant.isPinned)
};在这里,selfPartcipant.name或selfPartcipant.picture可以是零(这将代码中断)。当值为零时,我想放空字符串。
在javascript中等效的内容应该如下所示
const a = {
name: selfPartcipant.name || '',
picture: selfPartcipant.picture || '',
...other properties
}我怎样才能做到这一点?
发布于 2021-12-07 06:28:47
虽然Swift有一个零合并运算符??,但目标C没有,所以您最好的选择是使用这样的三元操作符:
NSDictionary *selfInfo = @{
@"id": selfPartcipant.id,
@"name": selfPartcipant.name ? selfPartcipant.name : @"",
@"picture": selfPartcipant.picture ? selfPartcipant.picture : @"",
@"audioEnabled": @(selfPartcipant.audioEnabled),
@"videoEnabled": @(selfPartcipant.videoEnabled),
@"isPinned": @(selfPartcipant.isPinned)
};公平的警告,这是我写过的最客观的C,所以不能保证如果你复制和粘贴它会工作,但这个想法应该是坚实的。
https://stackoverflow.com/questions/70255650
复制相似问题