我正在尝试调试我遇到的崩溃...
我从网络服务器获取了一些数据,所以我设置了三个类: 儿童 ChildConnection ChildParser
ChildConnection联系Web服务并获取数据并启动ChildParser,然后解析xml并将其保存为Child对象...
我已经让它在一个项目中工作了,而不是使用ChildConnection,我在AppDelegate中设置了连接,而我在当前项目中遇到的问题是与代理人有关(至少那是什么)我想)...因为我收到了错误: - [AppDelegate children]:无法识别的选择器发送到实例0x6b07e80
我很确定错误是由以下原因引起的:(注意:我对此很新)
- (ChildParser *) initChildParser {
self = [super init];
if(self)
{
childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate];
NSLog(@"Init");
}
return self;
}
ChildConnection.h:
@interface ChildConnection : NSObject
{
NSMutableArray *children;
NSMutableData *webData;
}
@property (nonatomic, retain) NSMutableArray *children;
-(void)connectionSetUp;
@end
ChildConnection.m:
#import "ChildConnection.h"
#import "ChildParser.h"
@implementation ChildConnection
@synthesize children;
- (void)connectionSetUp
{
NSString *soapMsg =
[NSString stringWithFormat:
Soap message left out due to sensitive data
];
NSURL *url = [NSURL URLWithString:@"Private"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
// Calculate the length of the post
NSString *postLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
// Set the headers
[req addValue:postLength forHTTPHeaderField:@"Content-Length"];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"PRIVATE" forHTTPHeaderField:@"SOAPAction"];
// Set the HTTP method and body
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(myConnection)
{
NSLog(@"Connection established");
webData = [NSMutableData data];
} else
{
NSLog(@"Connection failed");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse");
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//NSLog(@"didReceiveData");
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", [error localizedDescription]);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Finished loading");
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:webData];
//Initialize the delegate.
ChildParser *parser = [[ChildParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
/*
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
*/
//NSLog(@"Count: %@", [ count]);
}
@end
ChildParser.h:
@class Child;
@class ChildConnection;
@interface ChildParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentElementValue;
Child *aChild;
ChildConnection *childConnection;
}
- (ChildParser *) initChildParser;
@end
的.m:
#import "ChildParser.h"
#import "Child.h"
#import "ChildConnection.h"
@implementation ChildParser
- (ChildParser *) initChildParser {
self = [super init];
if(self)
{
childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate];
NSLog(@"Init");
}
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
NSLog(@"didstart");
if([elementName isEqualToString:@"GetKidsResult"])
{
// initialize the array
if(!childConnection.children)
{
childConnection.children = [[NSMutableArray alloc] init];
}
}
else if([elementName isEqualToString:@"a:KeyValueOfintKidf4KEWLbb"])
{
if(!aChild)
{
//Initialize the child.
aChild = [[Child alloc] init];
}
}
//NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSLog(@"foundcharacters");
/*
if(!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
}
else
{
[currentElementValue appendString:string];
}*/
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//NSLog(@"El name: %@", elementName);
if([elementName isEqualToString:@"GetKidsResult"])
{
NSLog(@"end of xml");
return;
}
if([elementName isEqualToString:@"a:KeyValueOfintKidf4KEWLbb"])
{
//NSLog(@"Found end of child");
//[childConnection.children addObject:aChild];
//NSLog(@"added");
//int i = [childConnection.children count];
//NSLog(@"Count: %d", i);
//aChild = nil;
}
else if([elementName isEqualToString:@"a:Key"])
{
//NSLog(@"Found key: %@", currentElementValue);
//aChild.key = [currentElementValue intValue];
//NSLog(@"key: %@", aChild.key);
}
else if([elementName isEqualToString:@"b:CPR"])
{
//NSLog(@"Found cpr");
//aChild.cpr = [currentElementValue intValue];
}
else if([elementName isEqualToString:@"b:CheckedIn"])
{
//NSLog(@"Found checkedIn");
//aChild.checkedIn = [currentElementValue boolValue];
}
else if([elementName isEqualToString:@"b:FirstName"])
{
//NSLog(@"Found firstname: %@", currentElementValue);
//[aChild setValue:currentElementValue forKey:@"firstName"];
//aChild.firstName = currentElementValue;
}
else if([elementName isEqualToString:@"b:Gender"])
{
//NSLog(@"found gender");
//aChild.gender = currentElementValue;
}
else if([elementName isEqualToString:@"b:Id"])
{
//NSLog(@"found id");
aChild.idChild = [currentElementValue intValue];
}
else if([elementName isEqualToString:@"b:IsOnTour"])
{
//NSLog(@"found isontour");
//aChild.isOnTour = [currentElementValue boolValue];
}
else if([elementName isEqualToString:@"b:LastName"])
{
//NSLog(@"found lastname: %@", currentElementValue);
//aChild.lastName = currentElementValue;
}
else if([elementName isEqualToString:@"b:GroupName"])
{
//NSLog(@"found groupname");
//aChild.groupName = currentElementValue;
}
currentElementValue = nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"didEndDocument");
//NSLog(@"Number of objects: %d", [childConnection.children count]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedParsing" object:nil];
}
@end
更新
好的,所以进一步了......我现在在使用数据的类中获得SIGABRT:
#import "AllView.h"
#import "CustomCellNoSubtitle.h"
#import "DTCustomColoredAccessory.h"
#import "Child.h"
#import "ChildConnection.h"
@implementation AllView
@synthesize allChildrenTable, childView, whichGroupLabel, charIndex;
-(void)receivedData
{
NSLog(@"data update gotten");
charIndex = [[NSMutableArray alloc] init];
listOfNames = [[NSMutableArray alloc] init];
for(int i=0; i<[childConnection.children count]-1; i++)
{
// get the person
Child *aChild = [childConnection.children objectAtIndex:i];
// get both first and last name and join them
NSString *joinName = [NSString stringWithFormat:@"%@ %@", aChild.firstName, aChild.lastName];
// save the full name to an array of all the names
[listOfNames addObject:joinName];
// get the first letter of the first name
NSString *firstLetter = [aChild.firstName substringToIndex:1];
NSLog(@"first letter: %@", firstLetter);
// if the index doesn't contain the letter
if(![charIndex containsObject:firstLetter])
{
// then add it to the index
NSLog(@"adding: %@", firstLetter);
[charIndex addObject:firstLetter];
}
}
[allChildrenTable reloadData];
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect the row, so it's clear when the user returns
[allChildrenTable deselectRowAtIndexPath:indexPath animated:YES];
if(self.childView == nil)
{
ChildView *cView = [[ChildView alloc] initWithNibName:@"ChildView" bundle:[NSBundle mainBundle]];
self.childView = cView;
}
[self.navigationController pushViewController:childView animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// set the number of sections in the table to match the number of first letters
return [charIndex count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// set the section title to the matching letter
return [charIndex objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// get the letter in each section
NSString *alphabet = [charIndex objectAtIndex:section];
// get the names beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];
return [names count];
}
// set up an index
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return charIndex;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCellNoSubtitle *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell = [[CustomCellNoSubtitle alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell.frame = CGRectZero;
}
/*
//---get the letter in the current section---
NSString *alphabet = [charIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];
if ([names count]>0) {
//---extract the relevant state from the states object---
NSString *cellValue = [names objectAtIndex:indexPath.row];
cell.primaryLabel.text = cellValue;
}
cell.myImageView.image = [UIImage imageNamed:@"kidblank.png"];*/
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate];
childConnection =[[ChildConnection alloc] init];
[allChildrenTable reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up a connection to the server to fetch the list of children
ChildConnection *childConnection = [[ChildConnection alloc] init];
[childConnection connectionSetUp];
// Set up a listener to receive notice when the parser is done
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData) name:@"finishedParsing" object:nil];
}
答案 0 :(得分:2)
问题看起来很简单。
在您的第一个粘贴函数中,您将应用程序委托分配给变量childConnection。
childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate];
当然,您确实希望将ChildConnection类的新实例分配给该属性。像这样:
childConnection = [[ChildConnection alloc] init];
我知道Objective-C错误有时很难理解,但你得到的错误实际上很清楚:
[AppDelegate children]: unrecognised selector
所以它抱怨你正在打电话给一个方法/财产&#34;孩子&#34;在应用程序委托上。但是,如果您不再使用它,为什么要在应用代表上调用任何内容?为什么你会称之为#34; children&#34;在它上面,当它实际定义为ChildConnection类的属性,而不是app delegate?
答案:因为你认为是ChildConnection的对象实际上是app delegate。
更新:看起来你需要在多个地方使用ChildConnection。最简单的方法是创建一个共享实例。将此方法添加到ChildConnection类:
+ (ChildConnection *)sharedConnection
{
static ChildConnection *sharedConnection = nil;
if (sharedConnection == nil)
{
sharedConnection = [[self alloc] init];
}
return sharedConnection;
}
现在,在其他课程中,只要您使用[[ChildConnection alloc] init]
而使用[ChildConnection sharedInstance]
代替。
答案 1 :(得分:2)
正确的违规代码是:
if(self)
{
childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate];
NSLog(@"Init");
}
原因是你将AppDelegate转换为ChildConnection对象,你不能这样做,因为它不是ChildConnection。
如果您想在AppDelegate中引用您的childConnection,我建议您使用以下内容:
+ (AppDelegate*)sharedDelegate;
//Implementation
+ (AppDelegate*)sharedDelegate {
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
所以这样你就像这样引用你的childConnection:
[AppDelegate sharedDelegate].childConnection;
如果您在App开始初始化childConnection时需要数据:
application:didFinishLaunchingWithOptions: