拿到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. 1.4---字符串空格变成20%(CC150)

    import CtCILibrary.AssortedMethods; public class Question { // Assume string has sufficient free spa ...

  2. 【转】【编码】ASCII 、UNICODE和UTF-8之二

    字符发展 1. 美国 ASCII-(American standard code information interchange) 美国信息互换标准代码 范围:1-127 ; 单字 备注:前部用作控制 ...

  3. HTML知识点链接

    1)<marquee>                    http://www.jb51.net/shouce/dhtml/objects/MARQUEE.html 2)"i ...

  4. Hexo

    Hexo Hexo is a fast, simple & powerful blog framework powered by Node.js.

  5. linux skill

    linux console终端乱码解决 1.console终端乱码 在/etc/profile文件的最后一行添加如下内容: export LC_ALL="zh_CN.GB18030" ...

  6. 允许FTP用户登录并禁止Shell登录的方法

    最近安装了vsftpd做FTP服务,发现系统用户的登录shell设置为/sbin/nologin,就无法使用FTP服务.网上资料说,vsftpd会为每个FTP登录用户去在/etc/shells中检查对 ...

  7. Effective C++ -----条款39:明智而审慎地使用private继承

    Private继承意味is-implemented-in-terms of(根据某物实现出).它通常比复合(composition)的级别低.但是当derived class需要访问protected ...

  8. Mathematics:Raising Modulo Numbers(POJ 1995)

    阶乘总和 题目大意:要你算一堆阶乘对m的模... 大水题,对指数二分就可以了... #include <iostream> #include <functional> #inc ...

  9. Mathematics:Prime Path(POJ 3126)

    素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的 ...

  10. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...