我编写了这个搜索程序,用于搜索包含文件名和计算机文件路径的对象数组。当我编写程序时,我有两个测试文件可以很好地处理我的代码。然后我开始添加实际文件,并且它可以正常使用10-20个文件,因此我决定添加更多(大约45个,总共大约60个文件)。这是出错了;该程序运行,但我无法搜索任何文件而没有获得"线程1:信号SIGABRT"错误,就行是我的搜索功能。我也在控制台中收到错误消息,但我担心自己无法理解。当我只有10个文件时,如果文件是可搜索的并不重要,当我使用所有文件时它就会停止工作。我在下面提供了我的代码:
SMADoc.h(用于创建PDF' s的对象的自定义类)
#import <Foundation/Foundation.h>
@interface SMADoc : NSObject
@property (nonatomic) NSNumber *docNumber
@property (nonatomic) NSString *urlToDoc;
@end
main.m(包含5个文件)
#import <Foundation/Foundation.h>
#import "SMADoc.h"
#include <readline/readline.h>
@import AppKit;
void *documentSearch() {
SMADoc *one = [[SMADoc alloc] init];
[one setdocNumber:@(17800)];
[one setUrlToDoc:@"/Users/Docs/docPath1.pdf"];
SMADoc *two = [[SMADoc alloc] init];
[two setdocNumber:@(11632)];
[two setUrlToDoc:@"/Users/Docs/docPath2.pdf"];
SMADoc *three = [[SMADoc alloc] init];
[three setdocNumber:@(17583)];
[three setUrlToDoc:@"/Users/Docs/docPath3.pdf"];
SMADoc *four = [[SMADoc alloc] init];
[four setdocNumber:@(14351)];
[four setUrlToDoc:@"/Users/Docs/docPath4.pdf"];
SMADoc *five = [[SMADoc alloc] init];
[five setdocNumber:@(11628)];
[five setUrlToDoc:@"/Users/Docs/docPath5.pdf"];
NSMutableArray *docs = [[NSMutableArray alloc] initWithObjects:one, two, three, four, five, nil];
int i = 0;
NSLog(@"Enter what you want to search for: ");
const char *searchC = readline(NULL);
int number = atoi(searchC);
NSNumber *sNumber = [NSNumber numberWithInteger:number];
for (SMADoc *nSearch in docs) {
if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
NSLog(@"Opening document...");
[[NSWorkspace sharedWorkspace] openFile:[nSearch urlToDoc]];
}
if (![sNumber isEqualToNumber:[nSearch docNumber]]) {
i++;
}
}
if (i == [docs count]) {
NSLog(@"A match could not be found, please check your spelling");
}
free(documentSearch());
documentSearch();
return 0;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"message");
documentSearch();
}
return 0;
}
下面这一行得到了SIGABRT错误:
if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
这是控制台中的输出:
2015-07-09 14:28:25.047 LIX4[3537:108937] message
2015-07-09 14:28:25.049 LIX4[3537:108937] Enter what you want to search for:
1177880000
2015-07-09 14:28:27.505 LIX4[3537:108937] Opening document...
2015-07-09 14:28:27.633 LIX4[3537:108937] An uncaught exception was raised
2015-07-09 14:28:27.633 LIX4[3537:108937] -[__NSCFNumber compare:]: nil argument
2015-07-09 14:28:27.633 LIX4[3537:108937] (
0 CoreFoundation 0x00007fff8b8ae03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8ef9076e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8b8adeed + [NSException raise:format:] + 205
3 CoreFoundation 0x00007fff8b7ab031 - [__NSCFNumber compare:] + 81
4 CoreFoundation 0x00007fff8b7aafc8 - [__NSCFNumber isEqualToNumber:] + 24
5 LIX4 0x0000000100004a3a documentSearch + 14490
6 LIX4 0x0000000100005355 main + 53
7 libdyld.dylib 0x00007fff8fb6f5c9 start + 1
)
2015-07-09 14:28:27.634 LIX4[3537:108937] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8b8ae03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8ef9076e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8b8adeed +[NSException raise:format:] + 205
3 CoreFoundation 0x00007fff8b7ab031 -[__NSCFNumber compare:] + 81
4 CoreFoundation 0x00007fff8b7aafc8 -[__NSCFNumber isEqualToNumber:] + 24
5 LIX4 0x0000000100004a3a documentSearch + 14490
6 LIX4 0x0000000100005355 main + 53
7 libdyld.dylib 0x00007fff8fb6f5c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
如果有人能帮我解决这个问题,我将非常感激!
答案 0 :(得分:0)
您的对象是否包含数字? ,我感觉
[nSearch docNumber]
必须为零,即它崩溃
答案 1 :(得分:0)
不是你的问题的答案,而是在关于无限循环的评论中引用我们的对话。你的是递归的,永远不会结束。到时候你会遇到堆栈溢出。 这是达到同样目的的正确方法:
int main(int argc, const char * argv[]) {
while (true) {
@autoreleasepool {
documentSearch();
}
}
return 0;
}
请注意@autoreleasepool
在循环内。
答案 2 :(得分:0)
我解决了! MacRumors的人帮助我解决了我的问题!我打开了我的程序的两个版本;有60个文件不断崩溃的文件和有5个文件的文件(我在这里发布的代码)并设置了一个断点
if ([sNumber isEqualToNumber:[nSearch docNumber]])
在两个版本中然后检查变量,我发现了一些错误的东西。我发现sNumber
变量的值(用户输入的内容)有时会存储在double
而不是int
中,这就是问题!我通过使用变量号而不是sNumber解决了这个问题(因为那已经是一个int)并替换了这段代码:
if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
[...]
if (![sNumber isEqualToNumber:[nSearch docNumber]]) {
用这个:
if (number == [[nSearch docNumber] intValue]) {
[...]
(!(number == [[nSearch docNumber] intValue])) {
这解决了这个问题!
编辑:如果有兴趣的话,这是MacRumors线程的链接:http://forums.macrumors.com/threads/comparing-two-numbers-causes-error.1900677/