フィードから文字列を解析し、それらを URL に変換して、FeedItem のプロパティとして保存しています。最初は正常に URL に変換されて保存されますが、後でプロパティにアクセスすると nil になります。
FeedItem.h
@interface FeedItem : NSObject
@property (nonatomic, strong) NSString* author;
@property (nonatomic, strong) NSURL* imageURL;
@property (nonatomic, strong) NSString* datePublished;
@end
パーサー.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];
}
}
これは毎回「is nil」と出力します。
解析される URL の 1 つの例を次に示します。
URL に特殊文字が含まれていると問題が発生する可能性があることはわかっていますが、最初は成功しているので、これは問題ではないと考えました。
私はobjective-cが初めてです...何が欠けていますか??