Objective-c对集合的操作实在在麻烦了,特别是对于复杂的集合 - V2EX
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
refresh
V2EX    iDev

Objective-c对集合的操作实在在麻烦了,特别是对于复杂的集合

  •  
  •   refresh 2013-09-05 21:58:22 +08:00 4766 次点击
    这是一个创建于 4421 天前的主题,其中的信息可能已经有所发展或是发生改变。
    数据是从JSON中转换过来的,所有有非常多的NSArray和NSDictionary,然后这些数据需要修改,然后我就疯了,一个个转换为NSMutable去改,JS处理这些东西是分分钟的事啊,简单要被弄疯了。
    例如有一个数据:
    var data = {
    user:[{
    role: [1, 3, 4],
    address:{
    city: "biejing"
    }
    }]
    }

    把这个JSON数据转换为dict,然后要修改其中的city为shanghai,js只需要data.user[0].address.city = 'shanghai'就行了,objective-c就疯了。

    有啥好招没,可能是我水平太烂,求教。
    34 条回复    1970-01-01 08:00:00 +08:00
    sharpnk
        1
    sharpnk  
       2013-09-05 22:06:55 +08:00   1
    为什么不给user做一个model,然后用你的raw json直接map过去?

    然后就是你的 [arrayName objectAtIndex:i].address.city
    txx
        2
    txx  
       2013-09-05 22:21:21 +08:00 via iPhone   1
    字典效率不如 model化啊。用字典是 不好的习惯 特别是在游戏里
    tgfbeta
        3
    tgfbeta  
       2013-09-05 22:24:25 +08:00
    [NSJSONSerialization JSONObjectWithData:data
    options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
    error:nil];
    refresh
        4
    refresh  
    OP
       2013-09-05 22:32:51 +08:00
    @sharpnk
    @txx
    确实应该model化,我现在也是model化,但是未彻底model化,当时是有其它考虑,但看来得不偿失了,谢谢提醒。


    @tgfbeta 这个是转换的时候转为Mutable吗,但并不能解决问题。
    refresh
        5
    refresh  
    OP
       2013-09-05 22:34:31 +08:00
    @sharpnk
    @tgfbeta 本来想省点事,结果发现更多事了,之前也想过model化,但因为某些原因只model化某一部分内容,浪费了我两天时间
    cloudream
        6
    cloudream  
       2013-09-05 22:43:29 +08:00   1
    ios7里objective-c可以和js互动了……

    JSContext* cOntext= [[JSContext alloc] init];
    [context evaluateScript:@"console.log(\"Hello Javascript\")"];
    tgfbeta
        7
    tgfbeta  
       2013-09-05 22:45:59 +08:00
    #import <Foundation/Foundation.h>
    NSString *jsOnString= @"{\"user\":[{\"role\": [1, 3, 4],\"address\":{\"city\": \"biejing\"}}]}";
    int main(int argc, const char * argv[])
    {

    @autoreleasepool {

    // insert code here...
    NSLog(@"Hello, World!");
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:data
    options:NSJSONReadingMutableContainers
    error:&err];
    dict[@"user"][0][@"address"][@"city"] = @"Shanghai";
    NSLog(@"%@",dict);

    }
    return 0;
    }
    tgfbeta
        8
    tgfbeta  
       2013-09-05 22:48:17 +08:00
    @cloudream 这太大杀器了也……
    refresh
        9
    refresh  
    OP
       2013-09-05 22:56:31 +08:00 via iPad
    @tgfbeta 这样也行?长知识了,这样和model化哪个好?

    @cloudream 那是以后的事了
    tgfbeta
        10
    tgfbeta  
       2013-09-05 23:0:46 +08:00
    @refresh model化是指使用CoreData模型?还是用@property存字符串和数据?
    容器的效率虽然没有这些高,但一般来说还过的去。
    主要看调用频率、数据量、内存占用状况、模型修改频率了。如果没有必要不需要优化。
    dorentus
        11
    dorentus  
       2013-09-05 23:15:13 +08:00
    Model framework for Cocoa and Cocoa Touch
    https://github.com/github/Mantle

    我最近的项目在用这个做 JSON dictionary <==> model class
    PrideChung
        12
    PrideChung  
       2013-09-06 00:10:28 +08:00
    Foundatin提供的数据结构要转换为mutable就一个mutableCopy的事,能有多麻烦,难道你不知道数组和字典的新下标语法么。
    alexrezit
        13
    alexrezit  
       2013-09-06 08:52:51 +08:00
    只能说明你菜, 不能说明它麻烦.
    refresh
        14
    refresh  
    OP
       2013-09-06 09:04:29 +08:00
    @alexrezit 我认同这个结论

    @PrideChung 我知道定义词典和NSArray可以用@{}与@[],也知道NSArray可以用[index]的方式取,但不知道dict也可以这样,确实方便很多。

    @tgfbeta 准备改成property的方式,有必要么?

    @dorentus 看上去似乎不错

    谢谢各位老师
    refresh
        15
    refresh  
    OP
       2013-09-06 09:11:23 +08:00
    再问一个菜鸟问题,对于JSON的字段名称,你们是专门建一个类,内建各种字段做影射,还是用define或者static呢?

    我现在是建一个类,然后用类方法返回字段的名称。比如说:
    1. json = {user: "uname"}
    2. mapping:

    +(NSString*) userField{
    return @"user";
    }

    或者用这样:
    static NSString *kJSOnFieldUser= @"user";
    或者:
    #define kJSONFieldUser @"user"

    各位老师,你们是怎么处理?

    @PrideChung
    @tgfbeta
    @alexrezit
    @cloudream
    alexrezit
        16
    alexrezit  
       2013-09-06 09:15:22 +08:00   1
    @refresh
    你是想用 ORM 么?
    refresh
        17
    refresh  
    OP
       2013-09-06 09:19:54 +08:00
    @alexrezit 不是想用ORM,对于JSON中的字段名,因为经常要用,直接用@"user"肯定不合适,所以固定下来。
    alexrezit
        18
    alexrezit  
       2013-09-06 09:21:37 +08:00
    @refresh
    那种我都是
    extern NSString * const kBlahBlahBlah;
    NSString * const kBlahBlahBlah = @"blah";
    refresh
        19
    refresh  
    OP
       2013-09-06 09:32:06 +08:00
    @alexrezit 这种方式不能在全局下使用啊,因为很多类都要用到
    alexrezit
        20
    alexrezit  
       2013-09-06 10:13:54 +08:00   2
    @refresh
    #import
    refresh
        21
    refresh  
    OP
       2013-09-06 10:37:57 +08:00
    @alexrezit 谢谢,感谢已发,你这种方式和#define哪个好,#define只要一行就搞定了。
    tgfbeta
        22
    tgfbeta  
       2013-09-06 16:01:20 +08:00   1
    @refresh 声明写在.h文件,作为从一个模块输出的符号。
    定义写在.m文件,全局唯一。
    PrideChung
        23
    PrideChung  
       2013-09-06 18:02:12 +08:00
    @refresh 我一般都选择自己把JSON返回的数据map成一个类。如果你确实要用字符串下标的话,用常量字符串,不要用宏定义。
    0day
        24
    0day  
       2013-09-06 21:28:09 +08:00
    对象化难免,自己做 Object Mapping !

    推荐的库:RestKit (https://github.com/RestKit/RestKit)
    alexrezit
        25
    alexrezit  
       2013-09-06 21:29:44 +08:00
    @0day
    - - 又把 RK 推出来坑人么...
    PrideChung
        26
    PrideChung  
       2013-09-06 21:31:17 +08:00
    @0day
    @alexrezit

    我看过RestKit以后不太喜欢,对于简单的Object Mapping实在太折腾了,自己写一个能有多难。
    0day
        27
    0day  
       2013-09-06 21:34:10 +08:00
    @PrideChung 不会多难,不用 Core Data 存储,不考虑 RESTful 的话,自己写就 OK 了。只是看写到多范型的问题。
    0day
        28
    0day  
       2013-09-06 21:34:53 +08:00
    @alexrezit 这东西慢慢啃还是挺有味道的
    lex
        29
    lex  
       2013-09-06 21:35:20 +08:00
    alexrezit
        30
    alexrezit  
       2013-09-06 21:40:37 +08:00
    @0day
    消化不良...
    PrideChung
        31
    PrideChung  
       2013-09-06 21:56:56 +08:00
    @0day
    @alexrezit

    RestKit那层层叠叠的类结构看得我头晕,另一个让我有这种感觉的就是Core Data,这两货加起来,OMG. 反正在移动设备上也不太会有很多复杂的数据,自己写写就完了。
    0day
        32
    0day  
       2013-09-06 21:59:02 +08:00
    @PrideChung 追求 lightweight 没错的,不过觉得结构不复杂这个,我倒是觉得要重新审视了!
    lovebirdegg
        33
    lovebirdegg  
       2013-09-06 22:33:37 +08:00
    弱弱的问一下 这个modle化是什么意思
    BB9z
        34
    BB9z  
       2013-09-25 13:01:49 +08:00
    可以试试 https://github.com/icanzilb/JSONModel ,我感觉比 Mantle 要好用,不过代码有点搓。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     963 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 22:35 PVG 06:35 LAX 15:35 JFK 18:35
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86