继续Get News List
拿到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 的设计人员估计也是醉了。
随机推荐
- 对xml文件的简单解析
package com.eprobj.demo; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; impor ...
- 调用pyxmpp库PyQt编程打包成exe文件出错
pyxmpp库内resolver.py内getaddrinfo获取Openfire服务器地址出错
- Tomcat端口占用
Tomcat端口占用问题 1.打开cmd,运行 netstat -aon!findstr "8080"查看该端口中运行的服务: 2.运行taskkill /pid PID /F 强 ...
- HTML——选择器
在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元素选择器和属性选择器允许您通过标签名.属性 ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- struts2 servlet api 访问方式
Action中访问ServletAPI. 主要就是接收表单参数及向域对象中存取值. 关于SevletAPI的方法在Action中有三种方式: 1.完全解耦合的形式: * 使用一个类:ActionCon ...
- 解读Unity中的CG编写Shader系列七(不透明度与混合)
转自http://www.itnose.net/detail/6098539.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段 ...
- iOS self = [super init]
self = [super init] 这个问题一直不太明白,今天研究了一下,在stackoverflow找到了下面的答案: http://stackoverflow.com/questions/29 ...
- Windows下安装Maven
上篇文章刚说到Linux下安装maven的过程,有时候为了适合在本地构建项目开发,然后上传到远程服务器执行,需要在本地构建maven项目,那么一般就是在Windows下构建maven项目并导入到我们的 ...
- 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...