拿到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. 【架构】MQTT/XMPP/GCM 等参考资料

    https://www.zhihu.com/question/29138530 https://segmentfault.com/q/1010000002598843/a-10200000026014 ...

  2. Debian安装python-rrdtool

    ... sudo apt-get install rrdtool sudo apt-get install librrd-dev sudo apt-get install python-dev pip ...

  3. git日志输出格式及两个版本之间差异列表

    查看commit id git log --pretty=format:"%h" git log --pretty=format:"%H" 获取两个版本间差异的 ...

  4. 1. EasyUI 学习总结(一)——对话框dialog

    文章参考来源:http://www.cnblogs.com/xdp-gacl/p/4075079.html 感谢博主的分享,写得非常精细,我在这边给看过的做一个记录. 一.EasyUI下载 使用eas ...

  5. Django~Models2

    Generally, each model maps to a single database table. Each attribute of the model represents a data ...

  6. suse linux 命令

    1.修改vftpd配置文件   vi /etc/vsftpd .conf                       #listen=YES   vi /etc/xinetd.d/vsftpd     ...

  7. mybatis 获取自增ID

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <inser ...

  8. 【剑指offer】题目36 数组中的逆序对

    数组中任取两个数字,如果前面的数字大于后面的数字称为一个逆序对 如:1,2,1,2,1 有3个逆序对 思路:知道O(N2)肯定是错的.开始想hash,试图找到O(n)的算法,想了很久,找不到.后来想到 ...

  9. c#简易计算器

    微软MSDN的代码库就有示例 http://code.msdn.microsoft.com/Simple-Calculator-54ec8e4a using System; using System. ...

  10. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...