2012-02-05 9 views
0

のインスタンス化におそらく、クラッシュ...のiOS:アプリケーションは、私が経験してるのクラッシュをデバッグしようとしているため、クラス

私は、Webサーバーからいくつかのデータを取得していますので、私は設定しました私が持っている

子供 ChildConnection ChildParser

ChildConnectionの連絡先、Webサービスやデータを取得し、XMLを解析し、子オブジェクトとして保存ChildParserを、開始します...:三つのクラスそれはプロジェクトで働いています。ここでは、ChildConnectionを持つ代わりに、私は接続を設定します私の現在のプロジェクトで問題となっているのは、デリゲートと関係することです(少なくとも私が思うものです)。エラーが発生してからです: - [AppDelegate children]:認識できないセレクタが送信されました(注:私はこれにかなり新しいです)

- (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インスタンス0x6b07e80

に私は、エラーが原因で発生していることかなり確信しています。 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 

UPDATE

わかりましたので、少しさらに...私は今、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]; 

} 
+2

申請代理人?それがChildConnectionのインスタンスでない限り、そこに問題があります。 –

+0

私は「やっていることを学んでいる」のですが、投稿されたコードは私の現在のケースで作業しようとしている別のプロジェクトの断片なので、恐らくChildParserクラスを間違って設定しています。代議員と一緒にいないと思います – user969043

+0

あなたの代議員と同じ考えです。あなたのログはクラッシュについて何を伝えていますか、関連する行を投稿できますか? –

答えて

2

問題は非常に簡単になります。

最初に貼り付けた関数では、アプリケーション代理人を変数childConnectionに割り当てています。

childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate]; 

きっとあなたはなく、実際にそのプロパティにChildConnectionクラスの新しいインスタンスを割り当てます。このように:

childConnection = [[ChildConnection alloc] init]; 

私はObjective-Cのエラーは時々の意味を理解するのは難しいことができることを知っているが、あなたが取得している一つは、実際にはかなり明確である:

[AppDelegate children]: unrecognised selector 

だから、あなたがしていることを訴えていますアプリデリゲートのメソッド/プロパティ "children"を呼び出します。しかし、なぜあなたはそれをもう使用していない場合は、アプリケーションの代理人の何かを呼び出すだろうか?なぜ、実際にはアプリケーションの代理人ではなく、ChildConnectionクラスのプロパティとして定義されているときに、「子」というメソッドを呼び出すのですか?

答え:ChildConnectionと思ったオブジェクトは、実際にはアプリケーションデリゲートです。

更新:ChildConnectionを複数の場所で使用する必要があるようです。これを行う最も簡単な方法は、共有インスタンスを作成することです。あなたのChildConnectionクラスにこのメソッドを追加します。あなたの他のクラスで今

+ (ChildConnection *)sharedConnection 
{ 
    static ChildConnection *sharedConnection = nil; 
    if (sharedConnection == nil) 
    { 
     sharedConnection = [[self alloc] init]; 
    } 
    return sharedConnection; 
} 

、あなたの代わりに[[ChildConnection alloc] init]使用[ChildConnection sharedInstance]を使用しましたどこ。

+0

です。さて、私はそれをchildConnection = [[ChildConnection alloc] init]に変更しました。確かにこれはトリックでした...しかし、私は今データを使用するクラスでアプリケーションがクラッシュしている...かなりそれは起こっているのと同様のものです... viewWillAppear私は持っていたchildConnection =(ChildConnection *)[[UIApplication sharedApplication ] delegate]; ....これを他のもののように変更しましたが、childConnection.childrenにアクセスしようとすると、SIGABRTでクラッシュします。 – user969043

+0

私が行った変更から新しい情報で質問を更新しました。 .. – user969043

+0

あなたはおそらく、各クラスで異なる子接続のインスタンスを使用したくないのですか?パーサで使用されているChildConnectionへの参照を取得し、ビューコントローラで同じインスタンスを使用できますか? –

2

あなたの右のコードの問題の一部は次のとおりです。

if(self) 
    { 
     childConnection = (ChildConnection *)[[UIApplication sharedApplication] delegate]; 
     NSLog(@"Init"); 
    } 

そして、あなたがChildConnectionオブジェクトへのあなたのAppDelegateを落としている理由も、それはChildConnectionではないので、あなたはこれを行うことはできません。

あなたのAppDelegateであなたのchildConnectionを参照したい場合、私は次のことをお勧めします。

+ (AppDelegate*)sharedDelegate; 

//Implementation 
+ (AppDelegate*)sharedDelegate { 
    return (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
} 

あなたがそうのようなあなたのchildConnectionを参照するので、この方法:

[AppDelegate sharedDelegate].childConnection; 

あなたはデータが必要な場合は瞬間アプリはchildConnectionの初期化を開始します:

application:didFinishLaunchingWithOptions: 
関連する問題