提问人:nabrugir 提问时间:5/7/2010 最后编辑:nabrugir 更新时间:7/19/2010 访问量:520
如何检查数组的内容?使用 ObjectiveC 解析 XML 文件
How can I check the content of the arrays? Parsing XML file with ObjectiveC
问:
我有 3 个班级>
video { nameVideo, id, description, user... }
topic {nameTopic, topicID, NSMutableArray videos; }
category {nameCategory, categoryID, NSMUtableArray topics}
然后在我的应用委托中,我定义了 NSMutableArray 类别>;
我用这段代码解析一个XML文件。我尝试了数组分层,我认为我没有在数组上添加任何对象。我该如何检查?怎么了?
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Videos"]) {
//Initialize the array.
appDelegate.categories = [[NSMutableArray alloc] init];
}
else if ([elementName isEqualToString:@"Category"])
{
aCategory = [[Category alloc] init];
aCategory.categoryID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id category value: %i", aCategory.categoryID);
}
else if ([elementName isEqualToString:@"Topic"]) {
aTopic = [[Topic alloc] init];
aTopic.topicID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id topic value: %i", aTopic.topicID);
}
else if ([elementName isEqualToString:@"video"]) {
aVideo = [[Video alloc] init];
aVideo.videoID = [[attributeDict objectForKey:@"id"] integerValue];
aVideo.nameTopic = currentNameTopic;
aVideo.nameCategory = currentNameCategory;
NSLog(@"Reading id video value: %i", aVideo.videoID);
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Videos"])
return;
if ([elementName isEqualToString:@"Category"]) {
[appDelegate.categories addObject:aCategory];
[aCategory release];
aCategory = nil;
}
if ([elementName isEqualToString:@"Topic"]) {
[aCategory.topics addObject:aTopic];
//NSLog(@"contador: %i", [aCategory.topics count]);
//NSLog(@"contador: %@", aTopic.nameTopic);
[aTopic release];
aTopic = nil;
}
if ([elementName isEqualToString:@"video"]) {
[aTopic.videos addObject:aVideo];
NSLog(@"count number videos: %i", [aTopic.videos count]); --> always 0
NSLog(@"NOM CATEGORIA VIDEO: %@", aVideo.urlVideo); --> OK
[aVideo release];
aVideo = nil;
}
if ([elementName isEqualToString:@"nameCategory"]) {
//[aCategory setValue:currentElementValue forKey:elementName];
aCategory.nameCategory = currentElementValue;
currentNameCategory = currentElementValue;
}
if ([elementName isEqualToString:@"nameTopic"]) {
aTopic.nameTopic = currentElementValue;
currentNameTopic = currentElementValue;
}
else
[aVideo setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
xmlparser.h
@class TSCTerrassaAppDelegate, Category, Topic, Video;
@interface XMLParser : NSObject {
NSMutableString *currentElementValue;
NSMutableString *currentNameCategory;
NSMutableString *currentNameTopic;
TSCTerrassaAppDelegate *appDelegate;
Video *aVideo;
Topic *aTopic;
Category *aCategory;
}
@property (nonatomic, retain)NSMutableString *currentNameCategory;
@property (nonatomic, retain) NSMutableString *currentNameTopic;
视频.h
@interface Video : NSObject {
NSInteger videoID;
NSString *nameVideo;
NSString *nameCategory;
NSString *nameTopic;
NSString *user;
NSString *date;
NSString *description;
NSString *urlThumbnail;
NSString *urlVideo;
}
主题.h
@class Video;
@interface Topic : NSObject {
NSMutableString *nameTopic;
NSInteger topicID;
NSMutableArray *videos;
}
@property (nonatomic, readwrite) NSInteger topicID;
@property (nonatomic, retain) NSMutableArray *videos;
@property (nonatomic, retain) NSMutableString *nameTopic;
Category.h
@class Topic, Video;
@interface Category : NSObject {
NSMutableString *nameCategory;
NSInteger categoryID;
NSMutableArray *topics;
}
@property (nonatomic, retain) NSMutableArray *topics;
@property (nonatomic, readwrite) NSInteger categoryID;
@property (nonatomic, retain) NSMutableString *nameCategory;
AppDelegate
@class NavegaNavController;
@interface TSCTerrassaAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *rootController;
IBOutlet NavegaNavController *navegaNavController;
NSMutableArray *categories;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@property (nonatomic, retain) IBOutlet NavegaNavController *navegaNavController;
@property (nonatomic, retain) NSMutableArray *categories;
下面是XML文件的简短示例:
<Videos>
<Category id="12">
<nameCategory>xxxx</nameCategory>
<Topic id="43">
<nameTopic>zzzz</nameTopic>
<video id="54">
<nameVideo>videoest1</nameVideo>
<user>skiria</user>
<date>24-11-09</date>
<description>testing videos</description>
<urlThumbnail>URLTHUMBNAIL</urlThumbnail> <urlVideo>http://bipbop/gear1/prog_index.m3u</urlVideo>
</video>
</Topic>
</Category>
</Videos>
答:
0赞
user121301
5/7/2010
#1
看起来 aTopic.videos 从未被分配/初始化。
在 didStartElement 中创建 aTopic 后,立即对 aTopic.videos 执行 alloc+init。
评论
0赞
nabrugir
5/7/2010
我添加这个 aTopic.videos = [[NSMutableArray alloc] init];在didStartElement上,但问题是一样的。计数返回 0。HEre 是我添加的地方: else if ([elementName isEqualToString:@“Topic”]) { aTopic = [[Topic alloc] init]; aTopic.videos = [[NSMutableArray alloc] init]; aTopic.topicID = [[attributeDict objectForKey:@“id”] integerValue];NSLog(@“读取 id 主题值: %i”, aTopic.topicID);}
0赞
5/7/2010
在记录计数的地方,也只记录 aTopic(例如 NSLog(“aTopic = %@”, aTopic);)以确保它不是 nil。也许它会在视频元素结束之前发布。
0赞
nabrugir
5/8/2010
我得到<主题:0x383d710> <主题:0x383c540>...每个主题元素都有不同的值。这是什么意思?错误可能在哪里?
0赞
5/8/2010
所以主题不是零。它们是不同的,因为您为每个 Topic 元素创建一个新的 aTopic。不知道问题出在哪里。发布一个简短但完整的 xml 示例(将其添加到问题中)。单步执行调试器中的代码也可能对你有帮助。
0赞
5/8/2010
发现另一个问题:创建 aCategory.topics 时也无法创建。在 didStartElement 中 aCategory 的 alloc+init 之后添加以下行:aCategory.topics = [[NSMutableArray alloc] init];
评论