J'ai une méthode de démarrage qui est connectée à un bouton dans IB, et fondamentalement, la première fois que je l'exécute, tout fonctionne bien, mais quand je l'exécute la deuxième fois (et je vous assure que rien d'autre ne se passe les données entre), j'obtiens un EXC_BAD_ACCESS après la première boucle (int i). Je me suis tourné sur NSZombieEnabled et il ne me dit rien, je viens simplement obtenir une plaineEXC_BAD_ACCESS signal
Programme signal reçu: « EXC_BAD_ACCESS ». SharedLibrary apply-charge Toutes les règles
Voici la méthode:
- (IBAction)start:(id)sender {
NSLog(@"1");
NSArray* firstArr = [data objectAtIndex:0];
NSLog(@"2");
for (int i=1; i < [data count]; i++) {
NSLog(@"%@", data);
NSArray* currArray = [data objectAtIndex:i];
NSString* fileName = [currArray objectAtIndex:[firstArr indexOfObject:@"UseFile"]];
NSString* filePath = [NSString stringWithFormat:@"/%@", fileName];
NSString* saveAs = [currArray objectAtIndex:[firstArr indexOfObject:@"SaveFileAs"]];
NSLog(@"3");
for (int j=0; j < [firstArr count]; j++) {
NSLog(@"4");
if ([self isIndexIdentifier:j]) {
NSLog(@"5");
NSString* searchStr = [firstArr objectAtIndex:j];
NSString* replaceStr = [currArray objectAtIndex:j];
NSDictionary* error;
NSLog(@"6");
NSString* appleScript = [NSString stringWithFormat:
@"set searchstring to \"%@\"\n"
@"set replacestring to \"%@\"\n"
@"tell application \"QuarkXPress\"\n"
@"activate\n"
@"if (not (exists document \"%@\")) then\n"
@"open POSIX file \"%@\" with Suppress All Warnings\n"
@"end if\n"
@"tell document \"%@\"\n"
@"repeat with tb from 1 to count of text box\n"
@"tell text box tb\n"
@"set (every text where it is searchstring) to replacestring\n"
@"end tell\n"
@"end repeat\n"
@"end tell\n"
@"end tell\n",
searchStr, replaceStr, fileName, filePath, fileName];
NSLog(@"7");
NSLog(@"%@", appleScript);
NSLog(@"8");
NSAppleScript *script = [[NSAppleScript alloc] initWithSource: appleScript];
NSLog(@"9");
[script executeAndReturnError:&error];
NSLog(@"10");
[script release];
NSLog(@"11");
NSLog(@"%@", error);
}
}
}
}
Je NSLogged pour voir si peut-être je peux trouver quelque chose et est ici la console la deuxième fois la méthode est appelée:
run
2011-04-02 08:55:38.145 TestUI[4472:a0f] 1
2011-04-02 08:55:38.145 TestUI[4472:a0f] 2
2011-04-02 08:55:38.146 TestUI[4472:a0f] (
(
UseFile,
xxxxxxxxxxxxx,
SaveFileAs
),
(
"1.qxp",
11111,
""
),
(
"2.qxp",
aslkvknv,
""
),
(
"3.qxp",
ABCDEFG,
""
),
(
"4.qxp",
222222222,
""
),
(
"5.qxp",
asdf,
adsffdsa
)
)
2011-04-02 08:55:38.146 TestUI[4472:a0f] 3
2011-04-02 08:55:38.147 TestUI[4472:a0f] 4
2011-04-02 08:55:38.147 TestUI[4472:a0f] 4
2011-04-02 08:55:38.147 TestUI[4472:a0f] 5
2011-04-02 08:55:38.147 TestUI[4472:a0f] 6
2011-04-02 08:55:38.148 TestUI[4472:a0f] 7
2011-04-02 08:55:38.148 TestUI[4472:a0f] set searchstring to "xxxxxxxxxxxxx"
set replacestring to "11111"
tell application "QuarkXPress"
activate
if (not (exists document "1.qxp")) then
open POSIX file "/1.qxp" with Suppress All Warnings
end if
tell document "1.qxp"
repeat with tb from 1 to count of text box
tell text box tb
set (every text where it is searchstring) to replacestring
end tell
end repeat
end tell
end tell
2011-04-02 08:55:38.148 TestUI[4472:a0f] 8
2011-04-02 08:55:38.148 TestUI[4472:a0f] 9
2011-04-02 08:55:38.517 TestUI[4472:a0f] 10
2011-04-02 08:55:38.517 TestUI[4472:a0f] 11
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)
+1, essayez d'abord de définir l'erreur à zéro lors de sa création. – jv42
Merci à vous deux, c'était le problème! Je l'ai commenté et il ne s'est pas crashé, mais comment puis-je savoir si une erreur s'est produite alors? J'ai essayé si (erreur! = Nil) nslog .... mais il se bloque. Dois-je mettre l'erreur à zéro quand je la crée? N'est-ce pas automatiquement nul? Si ce n'est pas nul, alors à quoi cela sert-il? Quoi qu'il en soit, merci encore une fois. – user635064
Vous devez le définir à zéro lorsque vous le créez. Ce n'est pas automatiquement nul; c'est vrai seulement des variables membres, pas des variables locales. Les variables non initialisées sont définies sur n'importe quelle valeur aléatoire se trouvant dans cette partie de la mémoire, ce qui explique pourquoi vous avez obtenu des résultats différents sur différentes exécutions. – smorgan