iOS - 解析Web URL的文件名/目录列表?

时间:2015-09-23 04:01:47

标签: ios filenames nsurlsession directory-structure

我想从提供的网址获取所有文件名和目录,以便我可以轻松地在我的应用中创建指向这些文件/目录的链接。

我无法找出解析使用NSURLSession的'dataTaskWithRequest'方法时返回的文本文件的最佳方法,因为它返回了url内容的所有数据(即权限,创建日期,所有者,组等)。 )。

以下是我从网址获取数据的方法:

    let request = NSURLRequest(URL: url)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) {[weak self] (data, response, error) -> Void in
        print("Data: \(data)")
        print("Response: \(response)")
        print("Error: \(error)")

        if let weakSelf = self, let data = data, let results = NSString(data: data, encoding: NSASCIIStringEncoding)
        {
            ...how do I parse out the file/directory names???...
        }
    }
    task.resume()

以下是从给定网址返回的字符串示例:

drwxrwxr-x   26 65534    65534       65536 Sep 20 03:29 Movies
drwxrwsr-x    3 0        65534       65536 Jul 09  2013 Network Trash Folder
drwxrwxr-x   41 65534    65534       65536 Aug 24 15:58 TV
drwxrwsr-x    3 0        65534       65536 Jul 09  2013 Temporary Items

我需要能够轻松准确地解析文件/目录名称,以便我可以创建指向它们的链接。建议?

1 个答案:

答案 0 :(得分:0)

假设您的字符串由换行符分隔,您可以通过以下方式解决此问题:

第1步:添加方法以在整个字符串中返回单词

- (NSString *)lastComponentFromString:(NSString *)iString {
    NSRange range= [iString rangeOfString: @" " options: NSBackwardsSearch];
    return  [iString substringFromIndex: range.location+1];   
}

第2步:添加方法以返回整个字符串但最后一个字

- (NSString *)stringAfterRemovingLastComponentFromString:(NSString *)iString {
    NSRange range= [iString rangeOfString: @" " options: NSBackwardsSearch];
    return [iString substringToIndex: range.location];
}

第3步:添加修剪前导和尾随空格的方法

- (NSString *)trimString:(NSString *)iBaseString {
    return (iBaseString && ![iBaseString isKindOfClass:[NSNull class]] && iBaseString.length > 0) ? [iBaseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : nil;
}

第4步:添加数字验证检查方法:

- (BOOL)numberValidation:(NSString *)text {
    NSString *regex = @"^([0-9]*|[0-9]*[.:][0-9]*)$";
    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [test evaluateWithObject:text];
    return isValid;
}

步骤5:添加一个方法,一次取一行,调用上面的方法并返回最终的文件夹名称。在这里,我们将继续查找文件夹,直到我们找到一个数字。

- (NSString *)folderNameForPathString:(NSString *)iPathString {
    NSString *lastComponent = [self lastComponentFromString:iPathString];
    NSString *remainingString = [self stringAfterRemovingLastComponentFromString:iPathString];
    NSString *finalFolder = @"";

    while (lastComponent.integerValue == 0) {
        NSString *initialSpaceString = [NSString stringWithFormat:@" %@", finalFolder];

    // Check if the next component is a number
    if ([self numberValidation:lastComponent]) {
        lastComponent = @"";
    }
        finalFolder = [lastComponent stringByAppendingString:initialSpaceString];
        lastComponent = [self lastComponentFromString:remainingString];
        remainingString = [self stringAfterRemovingLastComponentFromString:remainingString];
    }

    return [self trimString:finalFolder];
}

第6步:最后,解析服务器字符串,以便用新行字符分隔各个字符串。

NSString *data =  @"drwxrwxr-x   26 65534    65534       65536 Sep 20 03:29 Movies\ndrwxrwsr-x    3 0        65534       65536 Jul 09  2013 Network Trash Folder\ndrwxrwxr-x   41 65534    65534       65536 Aug 24 15:58 TV\ndrwxrwsr-x    3 0        65534       65536 Jul 09  2013 Temporary Items";

NSArray *lineArray = [data componentsSeparatedByString:@"\n"];
NSMutableArray *folders = [NSMutableArray array];

for (NSString *pathString in lineArray) {
    [folders addObject:[self folderNameForPathString:pathString]];
}

NSLog(@"folders = %@", folders); // This prints - Movies, Network Trash Folder, TV, Temporary Items