我有一个方法启动,连接到IB的按钮,基本上,我第一次运行它,一切正常,但是当我第二次运行它(并且我向你保证没有任何其他事情发生数据之间),我得到一个EXC_BAD_ACCESS第一个循环后(int i)。我打开NSZombieEnabled,并没有告诉我什么,我只是简单的得到一个纯EXC_BAD_ACCESS信号
程序接收到的信号: “EXC_BAD_ACCESS”。 sharedlibrary 应用负载规则所有
这里的方法:
- (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);
}
}
}
}
我NSLogged,看看是否也许我能找到的东西,这里是控制台第二次该方法被称为:
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,首先尝试创建它时设置错误为零。 – jv42 2011-04-02 14:32:40
谢谢你们,那就是问题所在!我评论说它并没有崩溃,但是我怎么知道是否发生了错误呢?我试过如果(错误!=零)nslog ....但它崩溃。创建它时,我必须将错误设置为零吗?它不是自动零吗?如果它不是零,那么它是什么?无论如何,再次感谢。 – user635064 2011-04-02 15:44:11
创建它时应将其设置为零。它不会自动为零;这只适用于成员变量,而不是局部变量。未初始化的变量被设置为发生在该段内存中的任何随机值,这就是为什么在不同运行中得到不同结果的原因。 – smorgan 2011-04-02 15:56:40