2013-02-12 24 views
0

我正在解析來自Feed的字符串,將它們轉換爲URL並將它們存儲爲FeedItem的屬性。最初,它們已成功轉換爲URL並存儲,但稍後,當我訪問該屬性時,它是零。將字符串轉換爲NSURL並存儲爲屬性成功,稍後返回nil

FeedItem.h

@interface FeedItem : NSObject 

@property (nonatomic, strong) NSString* author; 
@property (nonatomic, strong) NSURL* imageURL; 
@property (nonatomic, strong) NSString* datePublished; 

@end 

Parser.m

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict 
{ 
// Custom blog object initialized here 
if ([elementName isEqualToString:@"entry"]) { 
    self.blogEntry = [[FeedItem alloc] init]; 
} 

// Parse image URL that accompanies some blog entries 
self.blogEntry.imageURL = [NSURL URLWithString:[attributeDict objectForKey:@"url"]]; 
if ([[NSURL URLWithString:[attributeDict objectForKey:@"url"]] isKindOfClass:[NSURL class]]) { 
    NSLog(@"converted to a url"); 

    if ([self.blogEntry.imageURL isKindOfClass:[NSURL class]]) { 
     NSLog(@"property of object is url"); 
    }else if (!self.blogEntry.imageURL) { 
     NSLog(@"url becomes nil"); 
    }else{ 
     NSLog(@"property of object is NOT url"); 
    } 
} 
} 

這版畫 「轉換爲一個URL」 和 「對象的屬性是URL」 每次它應該時間。但是,後來在同一個文件中:

- (void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName { 

if([elementName isEqualToString:@"entry"]) { 
    // An individual blog has been parsed and a pointer to it is added to the parsedResults array 

    if ([self.blogEntry.imageURL isKindOfClass:[NSURL class]]) { 
     NSLog(@"URL passed"); 
    }else if (!self.blogEntry.imageURL) { 
     NSLog(@"is nil"); 
    }else{ 
     NSLog(@"no luck"); 
    } 

    [self.parsedResults addObject:self.blogEntry]; 

} 
} 

這會打印出「無」每次。

這裏被解析URL的一個示例: URL =「http://2.bp.blogspot.com/-HlNeYKf6Jyk/URKJ0NzA_kI/AAAAAAAAADY/AAkM6mNITlo/s72-c/Ananya's+Illustration.jpg

我知道有可能是問題,如果一個URL具有特殊性格,但因爲它是成功的開始,我想通這不應該是問題。

我是新來的objective-c ...我在想什麼?

+0

如果不是如果(!self.blogEntry.imageURL)將其改爲else if(!self.blogEntry),它是否仍然記錄「爲零」? – rdelmar 2013-02-12 17:27:46

+0

@rdelmar不,它會記錄第三個選項「不幸」。剩下的屬性被正確存儲,我可以稍後訪問它們以顯示在UITableView中而沒有問題。 – mbrauh 2013-02-12 17:29:56

+0

你有這樣的地方:NSLog(@「object of object is url」);也可以嘗試記錄self.blogEntry.imageURL並查看它是否看起來像一個正確的URL。 – rdelmar 2013-02-12 17:45:13

回答

0

我的猜測是self.blogEntry正在取消分配。

這條線:

self.blogEntry = [[FeedItem alloc] init];

被替換什麼以前在self.blogEntry。在調用「didEndElement」方法之前,您的解析器是否有可能多次調用它?

或者是blogEntry指定爲strong屬性self?如果不是,它可能會在創建後在方法結束時被釋放。

+0

根據我所知,在找到結束標記之前,它不會多次調用它,因爲分配發生在找到開始標記時,並且在當前關閉之前不會有另一個標記(它調用「didEndElement 「)。 blogEntry被指定爲強 – mbrauh 2013-02-12 17:33:49

0

您的以下代碼片段並不能保證blogEntry對象是真正創建的。

// Parse image URL that accompanies some blog entries 
self.blogEntry.imageURL = [NSURL URLWithString:[attributeDict objectForKey:@"url"]]; 
if ([[NSURL URLWithString:[attributeDict objectForKey:@"url"]] isKindOfClass:[NSURL class]]) { 
    NSLog(@"converted to a url"); 

    if ([self.blogEntry.imageURL isKindOfClass:[NSURL class]]) { 
     NSLog(@"property of object is url"); 
    }else if (!self.blogEntry.imageURL) { 
     NSLog(@"url becomes nil"); 
    }else{ 
     NSLog(@"property of object is NOT url"); 
    } 
} 

您所創建的對象blogEntry只有當開始元素是「入口」,但你是塊外訪問它沒有任何檢查。當您嘗試設置圖像URL時,不保證會創建blogEntry對象。

雖然這並不能解釋爲什麼你的imageURL對象在didStartElement回調中有效,而不在didEndElement回調中。我只是說它很容易出錯。

+0

一旦找到第一個入口開始標記,blogEntry就不會始終存在?它是在找到開始標籤時創建的,但從未「銷燬」過......一旦找到另一個開始標籤,它就成爲一個新對象。不過,我明白了你的觀點......檢查此問題的最佳方法是什麼?就像'if(self.blogEntry!= nil)...'? – mbrauh 2013-02-13 05:24:09

0

在您的parser:didStartElement:namespaceURI:qualifiedName:attributes:方法中,您正在爲每個單個元素設置self.blogEntry.imageURL。某些元素不包含url屬性,並且當字典不包含特定鍵時,它會返回nil - 清除以前存儲在其中的任何值。

爲了克服這一點,你需要的代碼是這樣的:

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict 
{ 
    // Custom blog object initialized here 
    if ([elementName isEqualToString:@"entry"]) { 
     self.blogEntry = [[FeedItem alloc] init]; 
    } 

    // Only allow the element containing the url we want to effect the value 
    // of imageURL through 
    if ([elementName isEqualToString:@"media"]) { 

     self.blogEntry.imageURL = [NSURL URLWithString:[attributeDict objectForKey:@"url"]]; 
     if ([[NSURL URLWithString:[attributeDict objectForKey:@"url"]] isKindOfClass:[NSURL class]]) { 
      NSLog(@"converted to a url"); 

      if ([self.blogEntry.imageURL isKindOfClass:[NSURL class]]) { 
       NSLog(@"property of object is url"); 
      }else if (!self.blogEntry.imageURL) { 
       NSLog(@"url becomes nil"); 
      }else{ 
       NSLog(@"property of object is NOT url"); 
      } 
     } 

    } 
} 

考慮尋找到在Xcode斷點來幫助調試此類問題。

+0

只需插入你添加的if語句並沒有什麼區別 - 它從來都不是真的。我認爲這些網址並不在他們獨有的標籤中......你確定「媒體」是正確的嗎?是否有另一種方法來檢查圖片網址?另外,我玩弄斷點,但無法在調試窗口中顯示正在顯示的內容。我認爲檢查URL是否存在可能是關鍵,但... – mbrauh 2013-03-11 20:31:38

相關問題