我正在构建(尝试......)一个iPhone应用程序。 我的第一个用户故事是登录视图,他将用户的凭据发送到Web服务(WCF)。 与Web服务的通信过程工作正常,没有任何问题。 但我必须反序化来自它的信息。为此,我使用NSXMLParser创建包含服务响应的消息Object。这个问题何时开始:
这是消息解析器:
@interface MessageParser : NSObject <NSXMLParserDelegate> {
NSMutableString* currentProperty;
Message* message;
}
@property (nonatomic, retain) NSMutableString* currentProperty;
@property (nonatomic, retain) Message* message;
- (void)parseMessageData:(NSData *)data parseError:(NSError **)err;
@end
@implementation MessageParser
@synthesize message, currentProperty;
- (void)parseMessageData:(NSData *)data parseError:(NSError **)err
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
if (err && [parser parserError]) {
*err = [parser parserError];
}
[parser release];
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if(qName){
elementName = qName;
}
if([elementName isEqualToString:@"Message"]){
self.message = [[Message alloc] init];
}
else if( [elementName isEqualToString:@"body"] || [elementName isEqualToString:@"code"] || [elementName isEqualToString:@"error"]){
currentProperty = [NSMutableString string];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(qName){
elementName = qName;
}
if(message){
if([elementName isEqualToString:@"body"]){
self.message.messageBody = currentProperty;
}
else if([elementName isEqualToString:@"error"]){
self.message.error = currentProperty;
}
else if([elementName isEqualToString:@"code"]){
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
self.message.messageCode = [f numberFromString:currentProperty];
[f release];
}
}
self.currentProperty = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)foundedCharacters
{
if (self.currentProperty) {
[currentProperty appendFormat:@"%@", foundedCharacters];
}
}
这是登录操作:
- (IBAction)didLogin:(id)sender
{
NSData* receivedData;
NSError* parserError;
NSError* error;
receivedData = [[ServiceClient PerformLogin:txtLogin.text withPassword:txtPassword.text error:error] retain];
MessageParser* messageParser = [[MessageParser alloc] init];
Message* msg;
[messageParser parseMessageData:receivedData parseError:&parserError];
msg = [messageParser.message retain];
if(msg && !msg.error){
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Login Service Response" message:msg.messageBody delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if(msg.error){
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg.error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if(!msg){
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enable to contact the Athentication service." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
消息对象:
@interface Message : NSObject{
NSString* messageBody;
NSNumber* messageCode;
NSString* error;
}
@property (retain, nonatomic) NSString* messageBody;
@property (retain, nonatomic) NSNumber* messageCode;
@property (retain, nonatomic) NSString* error;
@end
@implementation Message
@synthesize error, messageBody, messageCode;
@end
那发生了什么事? 有时clik登录它会显示警报然后xcode在main.m中抛出EXC_BAD_ACCESS错误:
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
有些时候,当我点击登录时,它会直接给我带来同样的错误。
我的代码出了什么问题?我有一些内存管理问题吗?在哪里?
谢谢。
答案 0 :(得分:1)
将NSZombieEnabled : YES
放入您的项目&#34;编辑Active Executable&#34;部分。我希望能够帮助我们了解这个问题。
答案 1 :(得分:1)
您使用便捷方法而不保留结果....
currentProperty = [NSMutableString string];
要么......
currentProperty = [[NSMutableString string] retain];
或当您使用retain属性声明属性时使用...
self.currentProperty = [NSMutableString string];
另请注意,以下行正在泄漏内存....
self.message = [[Message alloc] init];
由于该属性还有一个retain属性,您需要释放您创建的版本...
Message *aMessage = [[Message alloc] init];
self.message = aMessage;
[aMessage release];
...或...
message = [[Message alloc] init];