顯示具有 Cocoa 標籤的文章。 顯示所有文章
顯示具有 Cocoa 標籤的文章。 顯示所有文章

2011年4月15日 星期五

NSNotificationCenter

- (void)addObserver:(id)observer selector:(SEL)aSelector :(NSString *)aName object:(id)anObject;
register to receive notification
parameter:
1. observer: the object to receive notification

2. aName: name of notification, if it is nil, we receive all notifications sent out by anObject

3. anObject: specify object we want to observe. If it is nil, we receive all notification with aName

we can register many objects to receive same notification.


- (void)postNotificationName:(NSString *)aName object:(id)anObject;
send notfication
parameter:
anObject: sender




2011年4月9日 星期六

responder chain in iOS & Cocoa

events are passed through responder chain

ex:
if first responder is view A
when view A does not handle event, it forward event to next responder, its view controller
if view controller  does not handle event, it forward event to view A's parent view B
if view B does not handle event, it forward event to its view controller
Hence,  sequence is
view A ->  view A's view controller -> view A's parent view B -> view A's parent view B's view controller
note:
if event is passed through all view hierarchy, it is then passed to app's window. If window does not handle event, it is passed to UIApplication object

forward an event:
ex:
[self.nextResponder processEvent:event];

2011年4月8日 星期五

operation is iOS SDK

Add operations into operation queue and queue will manage operation for us

operation type:
1. Block operation
2. Invocation operation
3. Plane operation

operation 執行的順序和加入operation queue的順序無關

2011年4月6日 星期三

2011年3月26日 星期六

custom delegate

兩個物件之間通訊的方式有很多,
透過自行訂義的@protocol是其中一種。
而我們往往將protocol名字取名為某種delegate。

將NSString變為合法的URL

使用

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc

ex:
url = [url   
      stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];



Download File Asynchronously

利用NSURLConnection的initWithRequest:delegate:startImmediately: method建立connection,
於startImmediately傳入yes表示馬上開始download
NSURL *url = [NSURL URLWithString:urlStr];
self.downloadData = [[NSMutableData alloc] init];
[self.downloadData release];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
self.downloadConnection = [[NSURLConnection alloc
                                   initWithRequest:req  
                                   delegate:self
                                   startImmediately:YES];
[self.downloadConnection release];


常用的NSURLConnectionDelegate method


開始download後,首先被呼叫的method
由於一個request可能收到多個response,
所以將data的長度設為0,
清除之前收到的data


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    [self.downloadData setLength:0];
}

將收到的data累加進downloadData
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"data %d", [data length]);
    [self.downloadData appendData:data];

}

成功完成下載時被呼叫
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Successfully connectionDidFinishLoading");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error %@", error);
}


測試輸出:

2011-03-26 17:50:00.690 DownloadAsync[13125:207] didReceiveResponse
2011-03-26 17:50:00.692 DownloadAsync[13125:207] data 3310
2011-03-26 17:50:00.693 DownloadAsync[13125:207] data 13258
2011-03-26 17:50:00.694 DownloadAsync[13125:207] Successfully connectionDidFinishLoading



Download File Synchronously

利用NSURLConnection的sendSynchronousRequest:returningResponse:error: method

NSString *urlStr = @"http://deeploveapple.blogspot.com/";
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req   
   returningResponse:nil error:&error];
if(data)
{
   NSString *str = [[NSString alloc] initWithData:data                                           
      encoding:NSUTF8StringEncoding];
   NSLog(@"str %@", str);
   [str release];
}
else
{
   NSLog(@"error %@", error);
}

利用NSXMLParser parse梁朝偉和劉德華

xml檔範例:



<?xml version="1.0" encoding="UTF-8"?>
    <永遠的偶像>
        <名字 name="梁朝偉">
            <音樂>為情所困</音樂>
            <movie>俠骨仁心</movie>
        </名字>
        <名字 name="劉德華">
            <音樂>來生緣</音樂>
            <movie>孤男寡女</movie>
        </名字>
    </永遠的偶像>
</xml>




利用xml檔案初始NSXMLParser物件
NSData *data = [NSData dataWithContentsOfFile:path];
xmlParser = [[NSXMLParser alloc] initWithData:data];


設定delegate,如此才能於NSXMLParserDelegate的method被呼叫時做處理
[xmlParser setDelegate:self];


 呼叫NSXMLParser物件的parse method開始進行parse
 parse method is a block call,做完才return
[xmlParser parse];
[xmlParser release];


以下三個method為我們最常使用的NSXMLParserDelegate method


1. 於遇到XML tag開頭時被呼叫,可取得tag的名稱以及tag裡的attribute



-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"didStartElement");
    NSLog(@"elementName %@", elementName);
    for(id key in attributeDict)
    {
        NSLog(@"attribute %@", [attributeDict objectForKey:key]);
    }
}



2. 找到XML tag所包含的內容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"foundCharacters %@", string);
}



3. 於遇到XML tag結尾時被呼叫


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"didEndElement");
    NSLog(@"elementName %@", elementName);    
}

測試輸出:
2011-03-26 16:04:46.172 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.175 XmlParse[12296:207] elementName 永遠的偶像
2011-03-26 16:04:46.177 XmlParse[12296:207] foundCharacters 
        
2011-03-26 16:04:46.178 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.179 XmlParse[12296:207] elementName 名字
2011-03-26 16:04:46.179 XmlParse[12296:207] attribute 梁朝偉
2011-03-26 16:04:46.180 XmlParse[12296:207] foundCharacters 
            
2011-03-26 16:04:46.181 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.181 XmlParse[12296:207] elementName 音樂
2011-03-26 16:04:46.182 XmlParse[12296:207] foundCharacters 為情所困
2011-03-26 16:04:46.183 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.184 XmlParse[12296:207] elementName 音樂
2011-03-26 16:04:46.185 XmlParse[12296:207] foundCharacters 
            
2011-03-26 16:04:46.185 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.186 XmlParse[12296:207] elementName movie
2011-03-26 16:04:46.187 XmlParse[12296:207] foundCharacters 俠骨仁心
2011-03-26 16:04:46.188 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.189 XmlParse[12296:207] elementName movie
2011-03-26 16:04:46.190 XmlParse[12296:207] foundCharacters 
        
2011-03-26 16:04:46.191 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.192 XmlParse[12296:207] elementName 名字
2011-03-26 16:04:46.193 XmlParse[12296:207] foundCharacters 
        
2011-03-26 16:04:46.194 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.195 XmlParse[12296:207] elementName 名字
2011-03-26 16:04:46.196 XmlParse[12296:207] attribute 劉德華
2011-03-26 16:04:46.196 XmlParse[12296:207] foundCharacters 
            
2011-03-26 16:04:46.197 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.198 XmlParse[12296:207] elementName 音樂
2011-03-26 16:04:46.198 XmlParse[12296:207] foundCharacters 來生緣
2011-03-26 16:04:46.199 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.200 XmlParse[12296:207] elementName 音樂
2011-03-26 16:04:46.201 XmlParse[12296:207] foundCharacters 
            
2011-03-26 16:04:46.202 XmlParse[12296:207] didStartElement
2011-03-26 16:04:46.202 XmlParse[12296:207] elementName movie
2011-03-26 16:04:46.203 XmlParse[12296:207] foundCharacters 孤男寡女
2011-03-26 16:04:46.204 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.204 XmlParse[12296:207] elementName movie
2011-03-26 16:04:46.206 XmlParse[12296:207] foundCharacters 
        
2011-03-26 16:04:46.206 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.207 XmlParse[12296:207] elementName 名字
2011-03-26 16:04:46.208 XmlParse[12296:207] foundCharacters 
    
2011-03-26 16:04:46.210 XmlParse[12296:207] didEndElement
2011-03-26 16:04:46.212 XmlParse[12296:207] elementName 永遠的偶像






NSData

+(id)dataWithContentsOfFile:(NSString *)path;
從檔案產生NSData物件

2011年3月25日 星期五

NSString & NSMutableString

-(NSString *)stringByAppendingString:(NSString *)aString;
將aString加到原來字串的結尾

NSDictionary & NSMutableDictionary

-(void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
otherDictionary裡的elements加入dic裡