• Das kreative Thema des Monats Oktober wird vor allem diejenigen unter Euch besonders ansprechen, denen Do-it-yourself quasi im Blut liegt. Zeigt uns Eure Improvisationskünste oder fotografiert, was andere Leute sich so im Alltag zusammengestrickt haben. --> Zum Mitmachen bitte hier klicken

Programm stürzt ab nach Verwendung von NSArray

jack.jack

Klarapfel
Registriert
20.03.09
Beiträge
280
Hallo
Ich verwende in meinem Programm einen NSMutableArray, wo ich verschiedene NSMutableDictionaries speichere:

NSMutableDictionary *myDownload = [NSMutableDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:downloadAddress,
absolutePath,
[absolutePath lastPathComponent],
nil]
forKeys:
[NSArray arrayWithObjects:@"address",
@"path",
@"name",
nil]];
[downloadsArray addObject:myDownload];


Bis dahin funktioniert es. Ich kann Objekte aus dem Array auslesen. Doch später wird in meinem Programm durch NSURLDownload die Funktion

- (void) downloadDidFinish: (NSURLDownload *)downloader {}

aufgerufen. Dort kann ich Objekte plötzlich nicht mehr aus dem Array auslesen, wenn ich es versuche, stürzt das Programm ab.
 
Wie liest du denn aus dem Array 'downloadsArray' aus?

Gruss ppocket

PS: Verwende in Zukunft bitte die CODE-Tags, danke!
 
Und Du hast downloadsArray auch initialisiert?

Also z.B. in init
Code:
[self setDownloadsArray:[NSMutableArray array]];
 
Was ist downloadsArray? Instanzvariable? Lokale Variable? Wo und wie initialisierst Du downloadsArray.

MacApple
 
Eine Instanzvariable, welche bei der Funktion
- (void)awakeFromNib
initialisiert wird.
 
Könntest du mal ein Wenig mehr Code posten? So kommen wir hier nicht weiter...
 
Kann leider nicht das ganze Programm posten. Aber hier ist mal der relevante Teil:

In AppController.h:
NSMutableArray *downloadsArray;
In AppController.m:
- (void) awakeFromNib {
downloadsArray = [[NSMutableArray alloc] init];
}
-(void) download {
NSMutableDictionary *myDownload = [NSMutableDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:downloadAddress,
absolutePath,
[absolutePath lastPathComponent],
nil]
forKeys:
[NSArray arrayWithObjects:@"address",
@"path",
@"name",
nil]];
[downloadsArray addObject:myDownload];
[self newDownloadComes];

}

- (void)newDownloadComes {
NSUInteger countOfMyDownloads;
countOfMyDownloads = [downloadsArray count];
if (countOfMyDownloads > 0) {
if (downloadFinish == YES) {
downloadFinish = NO;

NSMutableDictionary *myDownload = (NSMutableDictionary *)[downloadsArray objectAtIndex:0];

NSURLRequest *downloadRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: [myDownload objectForKey:@"address"]]];


NSURLDownload *videoDownload = [NSURLDownload alloc];

[videoDownload initWithRequest:downloadRequest delegate:self];

if (videoDownload) {
[videoDownload setDestination:[myDownload objectForKey:@"path"] allowOverwrite:YES];

}


}
}
}
- (void)downloadDidFinish:(NSURLDownload *)downloader {

[dataSourceOfTheTableView addObject:[(NSMutableDictionary *)[downloadsArray objectAtIndex:0] objectForKey:@"name"]];
}
 
Und bei welcher dieser Zeilen crashed es? Wieso meinst Du es läge daran, dass Du nicht mehr aus dem Array lesen kannst?

MacApple
 
Jo, diese Aussage hilft ja jetzt echt weiter. Viel Spaß dann noch.

MacApple
 
Mach doch sowas wie

Code:
NSLog(@"Array: %@\nArrayCount: %d", downloadsArray ,[downloadsArray count]);

in
Code:
- (void)downloadDidFinish:(NSURLDownload *)downloader

Ich glaube nämlich eher, dass das was IM Array steht futsch is.
 
Also ich seh da überhaupt kein Problem dabei mit deinem Array. Im Anhang findest du ein Projekt, bei dem mit deinem Code:

Code:
@interface UntitledAppDelegate (Private)

- (void)download;
- (void)newDownloadComes;

@end

@implementation UntitledAppDelegate

@synthesize window;

- (void) awakeFromNib {
	downloadsArray = [[NSMutableArray alloc] init];
}

-(void) download {
	NSString *downloadAddress = @"http://apple.com/", *absolutePath = [@"~/Desktop/Apple.html" stringByExpandingTildeInPath];
	
	NSMutableDictionary *myDownload = [NSMutableDictionary dictionaryWithObjects: [NSArray arrayWithObjects:downloadAddress, absolutePath, [absolutePath lastPathComponent], nil]
																		 forKeys: [NSArray arrayWithObjects:@"address", @"path", @"name", nil]];
	
	[downloadsArray addObject:myDownload];
	
	[self newDownloadComes];
	
}

- (void)newDownloadComes {
	if ([downloadsArray count] > 0) {
		NSMutableDictionary *myDownload = [downloadsArray objectAtIndex:0];
		NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[myDownload objectForKey:@"address"]]];
		NSURLDownload *videoDownload = [[NSURLDownload alloc] initWithRequest:downloadRequest delegate:self];
			
		if (videoDownload) {
			[videoDownload setDestination:[myDownload objectForKey:@"path"] allowOverwrite:YES];
		}
	}
}

- (void)downloadDidFinish:(NSURLDownload *)inDownloader {
	NSLog(@"Array: %@\nArrayCount: %d", downloadsArray ,[downloadsArray count]);
}

- (IBAction)doDownload:(id)inSender { [self download]; }

@end

ohne Probleme die Apple-Startseite auf den Desktop runterlädt. Aber ja, wenn du uns nicht mehr Code schicken kannst, können wir auch nicht weiterhelfen, ausser dir zu sagen, dass in diesem Code-Abschnitt kein Fehler zu finden ist!

Gruss ppocket
 

Anhänge

Ich habe mir dein Programm mal angeschaut. Aber genau so habe ich es gemacht. Werde bald ein bisschen mehr Code posten.
 
Wenn ich Informationen aus dem Array/Dictionary abrufe kommt folgendes in der Debugger Console:

2010-03-19 17:10:47.460 QuickTube[6283:a0f] -[NSCFOutputStream objectForKey:]: unrecognized selector sent to instance 0x116a708c0
 
Kann es sein, dass ich in dieser Zeile etwas falsch gemacht habe? (Voraussetzung dataSourceOfTheTable ist ein allocierter und initialisierter NSMutableArray)

[dataSourceOfTheTable addObject:[NSString stringWithString:[(NSMutableDictionary *)[downloadsArray objectAtIndex:0] objectForKey:@"name"]]];
 
Äh, kurze Frage, neben der Aussage, das wir echt mehr Code brauen und du das vielleicht noch mal in einem Post zusammenfassen könntes (ist echt unübersichtlich geworden!): Warum speicherst du dein Dictionary in dem MutableArray? Speicher doch direkt das Dictionary, das erspart dir dieses dumme Klammern auch an einigen vielen Stellen. Ich habe irgendwie im Gefühl, dass das einfach nicht "schön" ist und deswegen auch nicht funktioniert, dass du das Dictionary in einem sonst leeren MutableArray speicherst. Mache deine Varibale vom Typ NSMutableArray zu NSDictionary und dann guck was passiert..:)