Inserting nil objects into an NSDictionary

前端 未结 2 1236
借酒劲吻你
借酒劲吻你 2021-01-30 22:43

If we have an API that requires only 2 out of an objects 5 properties and iPhone app doesn\'t require them to instantiate an object, when the object is used in the params NSDici

2条回答
  •  忘掉有多难
    2021-01-30 23:28

    It's a bit long winded but you could do

    static id ObjectOrNull(id object)
    {
      return object ?: [NSNull null];
    }
    
    parameters:@{
      @"auth_token"         : ObjectOrNull(token),
      @"name"               : ObjectOrNull(drunk.name),
      @"date_started"       : ObjectOrNull(drunk.started_drinking),
      @"date_stopped"       : ObjectOrNull(drunk.stopped_drinking),
      @"prescribing_doctor" : ObjectOrNull(drunk.fat),
      @"pharmacy"           : ObjectOrNull(drunk.dumb),
    }
    

提交回复
热议问题