Hack4Life Hack4Life - 3 months ago 11
iOS Question

Extracting URLs from NSString into NSMutableString

I have a file with the following content:

cool name http://myurl1.tld/subdir/dir/var/ awesome\nanother cool name http://sub.domain.tld/dir/ nice\nsome nice name http://myname.tld/var/folder/ some\n


I read the file with this Objetive-C code:

NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"../files/name.ext"]];
NSString* raw = [[NSString alloc]
initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];


To extract all URLs from the above sample I use:

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:raw options:0 range:NSMakeRange(0, [raw length])];


My goal is to append all extracted URLs into one single string and separate them with a semicolon (without any other stuff from the file) with this loop:

NSMutableString *allURLStrings = [NSMutableString string];
for (NSString *s in matches) {
[allURLStrings appendString:s];
[allURLStrings appendString:@";"];
}


Whenever I try to run the code I get the following output:


terminating with uncaught exception of type NSException (lldb)


I tried to use the loop like this, but then the
substringForCurrMatch
only contains a slash (/):

NSMutableString * allRepoString = [NSMutableString string];
for (NSTextCheckingResult *s in matches) {
NSString* substringForCurrMatch = [s.URL path];
[allRepoString appendString:substringForCurrMatch];
[allRepoString appendString:@";"];
}


When I inspect
s
in the loop it shows me the correct object:
contenct of s

Is there a way to get this code working?

EDIT complete error message:

2016-08-11 23:00:19.272 AppName[34831:2892247] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[fetchurlsource allURLStrings]: unrecognized selector sent to instance 0x14c55ea00'
*** First throw call stack:
(0x181fa2db0 0x181607f80 0x181fa9c4c 0x181fa6bec 0x181ea4c5c 0x1000e8b6c 0x1000e8ee0 0x187100c40 0x187100844 0x18710759c 0x187104a88 0x18717afa4 0x1873a63ac 0x1873aa5f0 0x1873a7764 0x1839437ac 0x183943618 0x1839439c8 0x181f5909c 0x181f58b30 0x181f56830 0x181e80c50 0x18716f94c 0x18716a088 0x1000edb10 0x181a1e8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Answer

None of the code you posted so far can result in the exception you posted.

But the following code is incorrect:

NSMutableString * allRepoString = [NSMutableString string];  
for (NSTextCheckingResult *s in matches) {
    NSString* substringForCurrMatch = [s.URL path];
    [allRepoString appendString:substringForCurrMatch];
    [allRepoString appendString:@";"];
}

You do not want to call the path method on s.URL. To get the full URL as a string, use absoluteString. This will give you the URL as a string. path just gives you the path portion of the URL.

NSMutableString * allRepoString = [NSMutableString string];  
for (NSTextCheckingResult *s in matches) {
    NSString* substringForCurrMatch = [s.URL absoluteString];
    [allRepoString appendString:substringForCurrMatch];
    [allRepoString appendString:@";"];
}
Comments