2011年9月8日

XML Parser心得分享(2)-TBXML

另外一套是蠻多人推薦的TBXML
主要用法就是讀入檔案,然後用[TBXML elementName:element], [TBXML textForElement:element]取值
詳細可參照TBXML網站
因為開始第一次用,而且每次都要針對新的XML重新寫一遍delegate覺得很麻煩

就找了一下資料 ,後來參照了iOS下超级簡潔的xml解析框架:TBXML
修改了一下原始碼如下:

- (void)parse{
if (xmlData.rootXMLElement) {
TBXMLElement *element = xmlData.rootXMLElement;
[self recurrence:element];
}
else {
NSLog(@"Format Error!");
}
}

//遞迴
- (void)recurrence:(TBXMLElement *)element {
do {
//NSLog(@"<%@>:{%@}",[TBXML elementName:element], [TBXML textForElement:element]);
// Display the name of the element
if ([dic objectForKey:[TBXML elementName:element]]) {//如key name重複,命名為parent_chlid
[dic setObject:[TBXML textForElement:element] forKey:[NSString stringWithFormat:@"%@_%@",[TBXML elementName:element->parentElement],[TBXML elementName:element]]];
}
else{
[dic setObject:[TBXML textForElement:element] forKey:[TBXML elementName:element]];
}

//迭代處理所有屬性
TBXMLAttribute * attribute = element->firstAttribute;
while (attribute) {
//顯示
//NSLog(@"<%@>->[%@ = %@]", [TBXML elementName:element], [TBXML attributeName:attribute], [TBXML attributeValue:attribute]);
//迭代
attribute = attribute->next;
}

//遞迴處理子樹
if (element->firstChild) {
[self recurrence:element->firstChild];

}

//迭代處理sibling
} while ((element = element->nextSibling));

}

執行完會存成一個巢狀的NSDictionary結構
這樣應該可以處理稍微複雜一點、包含attribute的xml

2011年8月29日

XML Parser心得分享(1)-NSXML

有網友留言問到XML處理,就把之前的草稿完成一下
怕文章太長不好讀,拆成兩篇寫
寫到現在遇過挺多處理case要xml資料
目前用過的XML parser有內建的NSXML和TBXML


這篇先簡單說明一下NSXMLParserDelegate三個重要的函式:

2011年8月23日

兩個目前遇過的iOS 5 SDK crash bug

紀錄一下兩個升級iOS 5 SDK後遇到的error:


1. iOS 5好像還不太支援UIActivityIndicatorView
錯誤訊息:-[UIImage initWithCoder:]: unrecognized selector sent to instance 0xc974760
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage initWithCoder:]: unrecognized selector sent to instance 0xc974760'
參考自:http://stackoverflow.com/questions/7023430/uiimageview-initwithcoder-unrecognized-selector-sent-to-instance-ios-5-beta
解決方法:只好先把indicator移掉,或者用4.3 SDK跑就沒事

2.  一些專案不能用4.3 simulator跑
錯誤訊息:dyld: Library not loaded: /usr/lib/libc++abi.dylib ............. Reason: image not found
解決方法:先用5.0 simulator跑

debug了半天才知道是iOS 5的問題,目前還沒看到解法,看來只能等官方修正了
真的有案子要交的還是要準備兩套SDK阿阿阿

2011年8月16日

iOS 5關閉特定檔案的ARC

這篇文章參考以下影片完成:IOS5 -- How To Turn Off ARC On Individual Files
從iOS 4到iOS 5首先要面對的就是新增的automatic reference counting(第一次開專案看到release被deprecate有嚇到:P)
iOS 5新專案有ARC,但舊的code卻沒有,筆記一下關閉特定檔案ARC的作法:以(Xcode 4為例)
點選專案檔,選target,在Build Phase中找到compile source

2011年8月11日

NSArray排序筆記

本來以為這種功能要自己實做的
沒想到iOS SDK關於array的排序還挺簡單的
整理幾個基本的常用用法:
1.字串(NSString)、日期(NSDate)排序
2.使用NSSortDescriptor達到降冪排序
3.元素為自訂物件的排序