objc new features

私有变量声明

@implementation MyClass {
id ivar;
}

@interface MyClass () {
id ivar;
}

运行时检测是否支持某类

if ([NSJSONSerialization self]) {
if ([NSJSONSerializaiton isValidJSONObject:myObject]) {

}
}

Stronger Type checking
NSMutableArray *array = [[NSMutableSet alloc] init];
[array addObject:anyObject];
[array objectAtIndex:0]; //throws exception

KVO Tip

在对象被添加为anObject的Observer后在[anObject release]之前要先注销对象对anObject的Observing
比如
在[anObject addObserver:self forKeyPath:keypath];之后
在[self dealloc]时
要先用[anObject removeObserver:self forKeyPath:keypath];
再进行[anObject release];

TTModelViewController

TTModelViewController提供了一个机制来使View的展现与Model同步

TTModelViewController中设置了一系列标志位来存储Model与View的当前状态,通过UpdateViewState来更新View的内容

isModelXXXisInvalid的标记在相应操作生效后置1然后在调用回调后置0

isModelDidRefreshInvatrd  刷新后置1,执行完回调后置0
isModelWillLoadInvalid 在模型开始加载后置1
isModelDidLoadInvalid
isModelDidLoadFirstTimeInvalid
isModelDidShowFirstTimeInvalid
isViewInvalid
isViewSuspended 在ModelDidBeginUpdate和ModelDidEndUpdates中间为YES
isUpdatingView  updateView方法开始置1结束置0
isShowingEmpty
isShowingLoading
isShowingModel  己加载|!shouldLoad
isShowingError

私有方法:

resetViewStates:
判断几种视图的展示状态(加载中、展示模型、出错、空模型),如果开启则关闭.

updateViewStates
根据flag来更新视图的状态
在refresh中置isViewInvalid和isModelDidRefreshInvalid为NO

刷新的策略
从未加载过的Model使用内存+磁盘+网络缓存
加载过的Model使用网络缓存
判断为需要加载更多的使用内存+磁盘+网络缓存
其他的置isModelDidLoadInvalid为YES,如果View didAppear 则更新View([self updateView])

工作流程
在View加载前后设置 _isViewAppearing和_hasViewAppeared
viewWillAppear中在标记位设置后调用[self updateView]和父类viewWillAppear

– (void)updateView
在View无效的情况下如果View没有休眠也没有在更新中(isUpdatingView)则执行以下代码
设置isUpdatingView为YES
实例化Model
实例化View
[self updateViewStates]
如果当前控制器有冻结状态并且isShowingModel置YES则从_frozenState中恢复并结释放_frozenState
标记isViewInvalid和isUpdatingView为NO
[self reloadIfNeeded]

TTViewController会在viewWillAppear和viewDidAppear中暂停网络连接队列

从Three20中学到的一些方法

获取UIView的 Controller
使用 [UIResponder nextResponder]方法

http://developer.apple.com/library/mac/#qa/qa1490/_index.html
64位桌面程序个iOS程序的链接器中有一个Bug会使链接器中的-ObjC标识不起做用,Three20中使用了一个宏来结决这个问题
#define TT_FIX_CATEGORY_BUG(name) @interface TT_FIX_CATEGORY_BUG_##name @end \
@implementation TT_FIX_CATEGORY_BUG_##name @end
这里定义了一个类的实现来防止编译器忽略掉Catelog和不包含类的文件

Three20 UINavigator

Three20 UINavigator

TTGlobalNavigatorMetrics
屏幕方向 大小 导航大小 键盘大小 状态栏大小 工具栏大小


TTURLObject @protocol
NSString *URLValue
– (NSString*)URLValueWithName:(NSString*)name;


TTBaseNavigationController

– (void)pushViewController: (UIViewController*)controller
animatedWithTransition: (UIViewAnimationTransition)transition;
动画推入新的界面

– (UIViewController*)popViewControllerAnimatedWithTransition:(UIViewAnimationTransition)transition;
动画推出当前界面


TTNavigatorRootContainer 定义了导航对象的根控制器设定与获取控制器的导航对象方法
– (void)navigator:(TTBaseNavigator*)navigator setRootViewController:(UIViewController*)controller;
– (TTBaseNavigator*)getNavigatorForController:(UIViewController*)controller;


TTBaseNavigator
自动持久化ViewController
包含 URLMap,
读写 window, rootContainer
只读 rootViewController, visibleViewController, topViewController
读写 URL 打开或获取当前的URL
读写 persistenceMode 持久化控制器的方法,默认不缓存
persistenceExpirationAge 缓存时间

读写 supportsShakeToReload 是否支持摇晃刷新
读写 opensExternalURLs 是否支持打开外链
读写 isDelayed 是否有任务延迟加载

/**
* Determines the navigator that contains this view.
*
* Traverse the view hierarchy until the root view container is reached. If this root container
* conforms to the TTNavigatorRootContainer protocol, we call getNavigatorForController:
* with the top-most controller that contains this view that /isn’t/ the container.
* If getNavigatorForController: returns a navigator, this navigator is returned.
* Otherwise, the global navigator is returned.
*
* If the given view is not, in fact, a view, which is the case if a UIBarButtonItem is passed,
* returns the global navigator via [TTBaseNavigator globalNavigator].
*
* If you need to use a specific navigator for UIBarButtonItem, handle the button tap
* yourself and use navigatorForView: on an actual view in the controller.
*/
+ (TTBaseNavigator*)navigatorForView:(UIView*)view;
判断包含view的导航
如果在点击TabBarButton后要使用某个Navigator可以用此方法

+ (TTBaseNavigator*)globalNavigator;
+ (void)setGlobalNavigator:(TTBaseNavigator*)navigator;

打开URL
– (UIViewController*)openURLAction:(TTURLAction*)URLAction;
– (UIViewController*)openURLs:(NSString*)URL,…; 打开一系列URL,结果为最后的URL

获取URL对应的Controller
– (UIViewController*)viewControllerForURL:(NSString*)URL;
– (UIViewController*)viewControllerForURL:(NSString*)URL query:(NSDictionary*)query;
– (UIViewController*)viewControllerForURL:(NSString*)URL query:(NSDictionary*)query
pattern:(TTURLPattern**)pattern;

– (void)beginDelay;
初始化控制器可能非常占用耗时和资源,如果在动画的过程中发生会应响用户的体验
通过这个方法可以在让导航器在生成控制器后再动论

– (void)endDelay
– (void)cancelDelay

– (void)persistViewControllers;
持久化所有

– (UIViewController*)restoreViewControllers
恢复控制器

– (void)persistController:(UIViewController*)controller path:(NSMutableArray*)path;

– (void)removeAllViewControllers;
去除并释放

对象与URL的映射
– (NSString*)pathForObject:(id)object;
– (id)objectForPath:(NSString*)path;
– (void)resetDefaults; 清空持久化的数据

Three20 Core

TTGlobalCore

TTGlobalCore对Foundation Framework的扩展

创建不retain对象的集合 (通过对CFArray对象的retain, release赋以函数形式实现)
TTCreateNonRetainingArray()
TTCreateNonRetainingDictionary()

判断对象是否为集合
TTIsArrayWithItems(id object)
TTIsSetWithItems(id object)

判断字符串
TTIsStringWithAnyText(id object)

交换objc方法 (objc runtime)
TTSwapMethods(Class cls, SEL originalSel, SEL newSel)


TTGlobalCoreLocal
包含了与本地化相关的代码
TTCurrentLocal()
从standard user defaults中获取”AppleLanguages”(NSArray)中第一个的值,如果为空则调用[NSLocal currentLocal]

TTLocalizedString(NSString* key, NSString* comment)
从Three20.bundle中[bundle localizedStringForKey:key value:key table:nil]

TTDescriptionForError
如果error.domain为NSURLErrorDomain{本地化 NSURLErrorTimedOut, NSURLErrorNotConnectedToInternet 这两项错误,其它的显示为”Connection Error”的本地话字符串}

TTFormatInteger(NSInteger num)
以逗号分隔形式格式化整数


TTGlobalCorePaths
包函URL判断与设置默认Bundle及从默认bundle中取出资源的代码
TTIsBundleURL(NSString* URL) URL是否以”bundle://”开头
TTIsDocumentsURL(NSString* URL) URL是否以”document://”开头
TTSetDefaultBundle(NSBundle* bundle)
NSBundle* TTGetDefaultBundle()
NSString* TTPathForBundleResource(NSString* relativePath); 生成bundle的Resource目录中指定文件名的完整目录

NSString* TTPathForDocumentsResource(NSString* relativePath); 生成Documents目录中指定文件名的完整目录


TTGlobalCoreRects
包函与距形CGRect相关的函数
CGRect TTRectContract(CGRect rect, CGFloat dx, CGFloat dy)
CGRect TTRectShift(CGRect rect, CGFloat dx, CGFloat dy)
CGRect TTRectInset(CGRect rect, UIEdgeInsets insets)


NSArrayAdditions
两组遍历每个数组元素使其执行指定的方法
第一组 先复制自身再对复本执行方法,可能对数级元素进行修改
– (void)perform:(SEL)selector;
– (void)perform:(SEL)selector withObject:(id)p1;
– (void)perform:(SEL)selector withObject:(id)p1 withObject:(id)p2;
– (void)perform:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3;
第二组 不复制自身,禁止修改数据元素
– (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2;
– (void)makeObjectsPerformSelector: (SEL)selector
withObject: (id)p1
withObject: (id)p2
withObject: (id)p3;
查找方法
– (id)objectWithValue:(id)value forKey:(id)key;
– (id)objectWithClass:(Class)cls;
– (BOOL)containsObject:(id)object withSelector:(SEL)selector;


NSDataAdditions
两个Hash方法
– (NSString*)md5Hash
– (NSString*)sha1Hash


NSMutableArray
添加非空字串
– (void)addNonEmptyString:(NSString*)string


NSMutableDictionary
– (void)setNonEmptyString:(NSString*)string forKey:(id)key
为key添加非空字串值,不判断key是否为非空


NSObjectAdditions
对performSelector的扩展,允许最多带7个参数
– (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3;
– (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
withObject:(id)p4;
– (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
withObject:(id)p4 withObject:(id)p5;
– (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
withObject:(id)p4 withObject:(id)p5 withObject:(id)p6;
– (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
withObject:(id)p4 withObject:(id)p5 withObject:(id)p6 withObject:(id)p7;


NSStringAdditions
字串的判断
– (BOOL)isWhitespaceAndNewlines;
– (NSDictionary*)queryContentsUsingEncoding:(NSStringEncoding)encoding;
将URL查询字串转为字典,每个key对应一个数组,以应对有多个(相同的key)=value对,无value时value=[NSNull null]
– (NSString*)stringByAddingQueryDictionary:(NSDictionary*)query
在当前字串后添加query,过程中会对query的值做URL Encoding
– (NSString*)stringByRemovingHTMLTags;
– (NSComparisonResult)versionStringCompare:(NSString *)other;
* 示例 (?? 代表不确定):
* “3.0” = “3.0”
* “3.0a2” = “3.0a2”
* “3.0” > “2.5”
* “3.1” > “3.0”
* “3.0a1” < "3.0" * "3.0a1" < "3.0a4" * "3.0a2" < "3.0a19" <-- 不按字典顺序,按数字顺序, 只能用'a' * "3.0a" < "3.0a1" * "3.02" < "3.03" * "3.0.2" < "3.0.3" * "3.00" ?? "3.0" * "3.02" ?? "3.0.3" * "3.02" ?? "3.0.2" - (NSString*)md5Hash - (NSString*)sha1Hash

TTExtensionLoader
添加扩展方法:为TTExtentionLoader浅耕加Catelog
为方法名命为- (BOOL)loadExtensionNamed

+ (void)loadAllExtensions;
调用的唯一方法

+ (NSDictionary*)availableExtensions;

+ (NSDictionary*)loadedExtensions;
+ (NSDictionary*)failedExtensions;


TTEntityTables
XML entity Table ?


TTMarkupStripper
去标签