我正在解析提要中的字符串,将它们转换为 URL,并将它们存储为 FeedItem 的属性。最初,它们已成功转换为 URL 并存储,但后来,当我访问该属性时,它是 nil。
饲料项目.h
@interface FeedItem : NSObject
@property (nonatomic, strong) NSString* author;
@property (nonatomic, strong) NSURL* imageURL;
@property (nonatomic, strong) NSString* datePublished;
@end
解析器
- (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];
}
}
每次都会打印“is nil”。
这是正在解析的 URL 之一的示例: url=' http://2.bp.blogspot.com/-HlNeYKf6Jyk/URKJ0NzA_kI/AAAAAAAAADY/AAkM6mNITlo/s72-c/Ananya's+Illustration.jpg '
我知道如果 URL 具有特殊字符可能会出现问题,但是因为它一开始是成功的,所以我认为这不应该是问题。
我是objective-c的新手……我错过了什么?