V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Dystopia
V2EX  ›  iDev

大家都是怎么做「安全锁」的,比如服务器返回 json 每个都要判断是不是为 nil,数据多了好麻烦,有没有更好的办法

  •  
  •   Dystopia · 2016-08-23 19:01:00 +08:00 · 2475 次点击
    这是一个创建于 2802 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题,大家都是怎么做「安全锁」的,比如服务器返回 json 每个都要判断是不是为 nil ,数据多了好麻烦,有没有更好的办法。

    比如,一个 post 请求返回很多 string 数据,有过有数据为空 可能引起 App 崩溃,但当数据很多的时候 if else 判断真的很麻烦,有没有更有效的方法呢?
    6 条回复    2016-08-31 14:16:20 +08:00
    guomiaoyou7784
        1
    guomiaoyou7784  
       2016-08-23 19:10:29 +08:00
    可以扩展出 dictionary safe 方法。从 json 取用的时候判断对应类型是否需要提供默认值。数据为空引起 crash 是另一码事,代码不够健壮。
    tinyproxy
        2
    tinyproxy  
       2016-08-24 00:09:49 +08:00 via iPhone
    (°_°)…
    为什么空字符串会导致你的 APP 崩溃。。。
    Rubbly
        3
    Rubbly  
       2016-08-24 01:06:41 +08:00
    一个 post 请求返回很多 string 数据,感觉这个表达就有问题。
    顺吧请利用好 status code ,而且对于 NULL 也有自己的处理逻辑,与崩溃无关。
    hanxiV2EX
        4
    hanxiV2EX  
       2016-08-24 08:17:44 +08:00 via iPhone
    用 JSPNRPC 。从底层过滤掉错误数据。
    Tangdixi
        5
    Tangdixi  
       2016-08-30 20:28:40 +08:00 via iPhone
    发消息给 nil 不会 crash 的啊 插入 nil 才会
    ma125125t
        6
    ma125125t  
       2016-08-31 14:16:20 +08:00
    我一般直接判断字符串的 length
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1145 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:47 · PVG 06:47 · LAX 15:47 · JFK 18:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.