iOS接收null的处理方法

经常服务器返回的数据,有null,还有nil,如果在模型层不处理的话,到时候数据展现时,一定会崩啊,最近决心要解决这个问题,所以查看了一些资料后,有答案了:


- (id) setNoNull:(id)aValue{
    if (aValue == nil) {
        aValue = @"";//为null时,直接赋空
    } else if ((NSNull *)aValue == [NSNull null]) {
        aValue = @"";
        if ([aValue isEqual:nil]) {
            aValue = @"";
        }
    }
    return aValue;
}

这个方法,可以把null和nil赋空值,这样字符串操作时,就不会崩了,同时,如果解析成数值,也可以改写为@0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现Stripe、PayPal、iOS和Google的Webhook转发,需要进行以下步骤: 1. 配置Webhook:在Stripe、PayPal、iOS和Google开发者平台中配置Webhook,将需要接收的事件和对应的URL绑定。 2. 编写Controller:在Spring Boot项目中编写Controller,用于接收来自Webhook的事件和数据。 3. 编写Service:在Spring Boot项目中编写Service,用于处理接收到的Webhook事件和数据,并将其转发到其他系统。 具体实现步骤可以参考以下代码示例: Stripe Webhook转发: ```java @RestController @RequestMapping("/stripe") public class StripeWebhookController { @Autowired private StripeService stripeService; @PostMapping("/webhook") public ResponseEntity<Void> handleWebhook(@RequestBody String payload, @RequestHeader("Stripe-Signature") String signature) { stripeService.handleWebhook(payload, signature); return ResponseEntity.ok().build(); } } @Service public class StripeService { @Autowired private RestTemplate restTemplate; @Value("${webhook.forward.stripe}") private String forwardUrl; public void handleWebhook(String payload, String signature) { // 验证签名 Event event = Webhook.constructEvent(payload, signature, "Stripe API secret"); // 处理事件 // ... // 转发到其他系统 restTemplate.postForObject(forwardUrl, payload, Void.class); } } ``` 在上述代码中,StripeWebhookController用于接收来自Stripe Webhook的事件和数据,并调用StripeService进行处理和转发。在StripeService中,首先通过Stripe提供的工具类验证签名,然后处理事件,并将数据转发到其他系统。 PayPal Webhook转发和iOS Webhook转发的实现类似,只需要替换相关的代码和配置即可。 Google Webhook转发: ```java @RestController @RequestMapping("/google") public class GoogleWebhookController { @Autowired private GoogleService googleService; @PostMapping("/webhook") public ResponseEntity<Void> handleWebhook(@RequestBody String payload, @RequestHeader("Authorization") String authToken) { googleService.handleWebhook(payload, authToken); return ResponseEntity.ok().build(); } } @Service public class GoogleService { @Autowired private RestTemplate restTemplate; @Value("${webhook.forward.google}") private String forwardUrl; public void handleWebhook(String payload, String authToken) { // 验证授权 GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new GsonFactory()) .setAudience(Collections.singletonList("Google API client ID")) .build(); GoogleIdToken idToken = verifier.verify(authToken); if (idToken == null) { throw new RuntimeException("Unauthorized"); } // 处理事件 // ... // 转发到其他系统 restTemplate.postForObject(forwardUrl, payload, Void.class); } } ``` 在上述代码中,GoogleWebhookController用于接收来自Google Webhook的事件和数据,并调用GoogleService进行处理和转发。在GoogleService中,首先通过Google提供的工具类验证授权,然后处理事件,并将数据转发到其他系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值