Hello!
I have written a command-line tool that allows you to search for a property of a object (I created an custom class that allowed you to set the name of the file and the pathway to the file) and, if the search word matches the property, the file will open. When I did this with about 10 files it works as I want it to, but if I have around 60 files, I get a Thread 1: Signal SIGABRT error. The StackOverflow-community helped me figure out that this was because a certain value is equal to nil. I have not gotten any further answer on how to fix this on StackOverflow so I thought I would see if anyone here could help me!
Here is my code:
SMADoc.h (my custom class)
main.m
This is the line that gives the SIGABRT-error:
And this is the output in the console:
The StackOverflow-community helped me narrow down the problem to that it must be the
that is nil but after that I'm stuck...
I think that this is relevant in the error-output:
I tried googling this and what I got for answer is that the two variables/objects being compared must be the same type, but both
and
are NSNumbers so I don't see how that could apply here...
And this is were I am stuck... Any help would be greatly appreciated! Here is the link to the StackOverflow-post if anyone is interested:http://stackoverflow.com/questions/...ith-too-many-files-in-array/31320986#31320986
Thanks in advance!
I have written a command-line tool that allows you to search for a property of a object (I created an custom class that allowed you to set the name of the file and the pathway to the file) and, if the search word matches the property, the file will open. When I did this with about 10 files it works as I want it to, but if I have around 60 files, I get a Thread 1: Signal SIGABRT error. The StackOverflow-community helped me figure out that this was because a certain value is equal to nil. I have not gotten any further answer on how to fix this on StackOverflow so I thought I would see if anyone here could help me!
Here is my code:
SMADoc.h (my custom class)
Code:
#import <Foundation/Foundation.h>
@interfaceSMADoc : NSObject
@property(nonatomic)NSNumber*docNumber
@property(nonatomic)NSString*urlToDoc;
@end
main.m
Code:
#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;
}
This is the line that gives the SIGABRT-error:
Code:
if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
And this is the output in the console:
Code:
2015-07-14 22:05:24.173 LIX4[512:11135] message
2015-07-14 22:05:24.175 LIX4[512:11135] Enter what you want to search for:
1717
2015-07-14 22:05:25.843 LIX4[512:11135] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff88eed03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8c5cf76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff88eeceed +[NSException raise:format:] + 205
3 CoreFoundation 0x00007fff88dea031 -[__NSCFNumber compare:] + 81
4 CoreFoundation 0x00007fff88de9fc8 -[__NSCFNumber isEqualToNumber:] + 24
5 LIX4 0x0000000100004a3a documentSearch + 14490
6 LIX4 0x0000000100005355 main + 53
7 libdyld.dylib 0x00007fff8d1ae5c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The StackOverflow-community helped me narrow down the problem to that it must be the
Code:
[nSearch docNumber]
I think that this is relevant in the error-output:
Code:
'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'
I tried googling this and what I got for answer is that the two variables/objects being compared must be the same type, but both
Code:
sNumber
Code:
[nSearch docNumber]
And this is were I am stuck... Any help would be greatly appreciated! Here is the link to the StackOverflow-post if anyone is interested:http://stackoverflow.com/questions/...ith-too-many-files-in-array/31320986#31320986
Thanks in advance!
Last edited: