编辑:我发现url是由一个字符串发送的,所以我把它改成了NSURL,现在它会解析一些数据,然后输入"NSUnknownKeyException“和SIGABRT。剩下的代码是一样的.谢谢!!
嘿,我正在尝试在iOS中运行一个简单的XML解析器。每次我运行它时,我都会在main中得到一个SIGABRT,错误是:NSInvalidArgument
我使用了一个简单的教程,下面是我的XMLParser
#import "XMLParser.h"
#import "Video.h"
@implementation XMLParser
@synthesize video, videos;
- (XMLParser *) initXMLParser {
//[super init];
// init array of user objects
videos = [[NSMutableArray alloc] init];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"video"]) {
NSLog(@"video element found – create a new instance of User class...");
video = [[Video alloc] init];
//We do not have any attributes in the user elements, but if
// you do, you can extract them here:
// user.att = [[attributeDict objectForKey:@"<att name>"] ...];
}
}
// XMLParser.m
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
}
NSLog(@"Processing value for : %@", string);
}
//XMLParser.m
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"videos"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:@"video"]) {
// We are done with user entry – add the parsed user
// object to our user array
[videos addObject:video];
// release user object
// [video release];
video = nil;
} else {
// The parser hit one of the element values.
// This syntax is possible because User object
// property names match the XML user element names
[video setValue:currentElementValue forKey:elementName];
}
// [currentElementValue release];
currentElementValue = nil;
}
@end
// end of XMLParser.m file这是我的video.h文件,用于了解我的数据类型。
#import <UIKit/UIKit.h>
@interface Video : NSObject {
NSInteger videoID;
NSString *videotitle; //Same name as the Entity Name.
NSString *description; //Same name as the Entity Name.
NSString *userid;
NSString *uploadtime;
NSString *channel;
NSInteger *viewed;
NSInteger *liked;
NSString *videosource;
NSString *smallthumbnail; //Same name as the Entity Name.
NSString *largethumbnail;
}
@property (nonatomic, readwrite) NSInteger videoID;
@property (nonatomic, retain) NSString *videotitle;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *userid;
@property (nonatomic, retain) NSString *uploadtime;
@property (nonatomic, retain) NSString *channel;
@property (nonatomic, readwrite) NSInteger *viewed;
@property (nonatomic, readwrite) NSInteger *liked;
@property (nonatomic, retain) NSString *videosource;
@property (nonatomic, retain) NSString *smallthumbnail;
@property (nonatomic, retain) NSString *largethumbnail;@end
还请注意,这是在iOs 5中实现的,下面是我的XML:
<videos>
<video id="1">
<videotitle>Lync - Find and Add a Contact</videotitle>
<description>Training tutorial for Lync</description>
<userid>USER.CORP_LDAP.i818800</userid>
<uploadtime>1321555939598</uploadtime>
<channel/>
<viewed>0</viewed>
<liked>0</liked>
<videosource>
https://saptube.pal.sap.corp/vod/media/FINAL_FindAndAddAcontact20111117105156.mp4
</videosource>
<smallthumbnail>
https://saptube.pal.sap.corp/vod/rtmp/FINAL_FindAndAddAcontact20111117105156.jpg
</smallthumbnail>
<largthumbnail>
https://saptube.pal.sap.corp/vod/rtmphd/FINAL_FindAndAddAcontact20111117105156.jpg
</largthumbnail>
</video>
</videos>发布于 2011-12-09 20:41:59
在您的视频类中,请实现以下函数:
- (void) setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"Missing key %@", key);
}然后,您可以查看您的类没有响应的键。
https://stackoverflow.com/questions/8373866
复制相似问题