拿到news list 所需要的技能

  • json数组反序列化
  • iOS中有哪些集合对象
  • 数组的遍历
  • Debugging with GDB

json数组反序列化

id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:data
                         options:NSJSONReadingAllowFragments

                         error:&error];
        if([jsonObject isKindOfClass:[NSArray class]])
        {
            NSArray *newsArray = (NSArray *)jsonObject;
            for (int i=0; i<[newsArray count]; i++) {
                id newsOjbect = newsArray[i];

                if([newsOjbect isKindOfClass:[NSDictionary class]])
                {
                    NSDictionary *deserializedDictionary = (NSDictionary *)newsOjbect;
                    News *news = [[News alloc]init];

                    [_newsList addObject:news];

                    if([deserializedDictionary objectForKey:@"Title"])
                    {
                        news.title=[deserializedDictionary objectForKey:@"Title"];
                    }
                    if([deserializedDictionary objectForKey:@"SubTitle"])
                    {
                        news.subTitle=[deserializedDictionary objectForKey:@"SubTitle"];
                    }
                }
            }
        }

iOS中有哪些集合对象

Array Objects

对象的有序集合,NSArray,NSMutableArray

{
    NSArfray *monthNames =[NSArray arrayWithObjects:@"January",@"Februay",@"March",nil];

    for(int i=0;i<12;i++)
    {
        NSLog (@"%@",[monthNames objectAtIndex : i]);
    }
}

当然,我们也有语法糖,

{
    NSArray *monthNames = @[@"Januar",@"February",@"March"];
    for(int i=0;i<12;i++)
    {
        NSLog(@"%@",monthNames[i]);
    }
}

@autoreleasepool
{
    NSMutableArray *numbers = [NUMutableArray array];
    for (i = 0; i<10; i++)
    {
        numbers[i] = @(i);
    }
}

Debugging with GDB

See here apple

Making an Address Book

Address Book 是address cards 的集合

Creat an Address Card

@interface AddressCard : NSObject

-(void) setName: (NSString *) theName;
-(void) setEmail: (NSString *) theEmail;

-(NSString *) name;
_(NSString *) email;

-(void) print;

@end;

-(void) setName: (NSString *) theName{
    name = [NSString stringWithString : theName];
}

@autoreleasepool{
    AddressCard *card1 = [AddressCard alloc]init];
}

当然,我们还是喜欢语法糖

@synthesize name,email;

The AddressBook Class

@interfact AddressBook : NSObject

-(instancetype) initWithName: (NSString *)name;

@end;

使用instancetype而不是id来作为构造函数或者工厂方法的返回值。当然,我原来是直接使用当前对象类型的。返回id谁都知道不太安全。

使用copy 和 strong 的简单的区别,一般来说我们操作的都是NSString, 两者无差。正常人也不太会用上NSMutableString, 用上时候该注意这两者的区别。

init

-(instancetype) initWithName: (NSString *)name
{
    self = [super init];
    if(self)
    {
        bookName = name;
        book = [NSMutableArray array];
    }

    return self;
}

-(instancetype) init
{
    return [self initWithName:@"NoName"];
}

遍历数组

-(void) list
{
    for(AddressCard *theCard in book)
        {
            .....
        }
}

有了这个基础,我们就可以改造上面的dirty code了。

 NSArray *newsArray = (NSArray *)jsonObject;

        for(NSDictionary *newsDictory in newsArray)
        {
            News *news = [[News alloc]init];

            [_newsList addObject:news];

            if([newsDictory objectForKey:@"Title"])
            {
                news.title=[newsDictory objectForKey:@"Title"];
            }
            if([newsDictory objectForKey:@"SubTitle"])
            {
                news.subTitle=[newsDictory objectForKey:@"SubTitle"];
            }
        }

NSValue

大家都懂的装箱和拆箱,这个是各种值类型都装到里面去。NSNumber 用来装数字,当然这个显然是NSValue 的子类。

字典的语法糖

if([newsDictory objectForKey:@"Title"])
            {
                news.title=[newsDictory objectForKey:@"Title"];
            }
            if([newsDictory objectForKey:@"SubTitle"])
            {
                news.subTitle=[newsDictory objectForKey:@"SubTitle"];
            }

我们改成

for(NSDictionary *newsDictory in newsArray)
        {
            News *news = [[News alloc]init];
            news.title =newsDictory[@"Title"];
            news.subTitle = newsDictory[@"SubTitle"];

            [_newsList addObject:news];
        }

断点的删除

前一阵子一直找不到如何快速删除断点,原来是可以直接拖拉去掉的。xcode 的设计人员估计也是醉了。

随机推荐

  1. 有关在线OJ网络AC爬虫

    搜索源码 爬取代码 自动登录 在线提交 判断AC

  2. ndk学习17: jni之Java调用C&C++

    一.Hello World 1. 定义函数原型 native关键字定义的函数即为jni函数 2.生成头文件 切换到src目录执行: (这个过程可以写脚本自动完成,比如自动拷贝到jni目录) javah ...

  3. espcms列表页ajax获取内容 - 并初始化swiper

    <link rel="stylesheet" href="swiper.min.css" type="text/css" media= ...

  4. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  5. MQTT V3.1----publish解读

    客户端/服务器的发布消息行为,与PUBLISH相关的消息类型: PUBLISH 客户端发布消息经由服务器分发到所有对应的订阅者那里.一个订阅者可以订阅若干个主题(Topic name),但一个PUBL ...

  6. struct vs class

    关于默认访问权限class中默认的成员访问权限是private的,而struct中则是public的. 关于继承方式class继承默认是private继承,而struct继承默认是public继承. ...

  7. ubuntu14.04和win7共享文件夹

    环境:vmware12 问题:安装了vmware-tools,但是在/mnt/hgfs下面看不到共享的文件夹. 按照网上的一些经验和教程使用如下命令: mount -t vmhgfs .host:/ ...

  8. 去掉mysql数据库字段中的个别字符

     update 表名 set 列名 = REPLACE (mcategory,"要去掉的字符","") where 列名 like "%要去掉的字符% ...

  9. [转]Android How to Download and Make Volley.jar

    原文来自:http://tips.androidhive.info/2015/08/android-how-to-download-and-make-volley-jar/   1 Comment . ...

  10. Debian 配置apt-get源

    1.配置apt-get源 cp  /etc/apt/sources.list  /etc/apt/sources.listbak   #备份原有配置文件       nano  /etc/apt/so ...