gpt4 book ai didi

ios - 循环遍历 URLS 的 NSArray - 崩溃 - iOS

转载 作者:行者123 更新时间:2023-11-28 18:19:27 25 4
gpt4 key购买 nike

我希望遍历我的 NSArray *urls - 对每个对象进行一些字符串调整。

但是,一旦它进入 For 循环并尝试调整 NSString *thing - 这会引发异常。

关于为什么我感到困惑有什么想法吗?

 - (void)GetData
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
directory = [NSString stringWithFormat:@"%@/%@/%@/%@", directory, self.researcherID, self.participantID, testTypeString];
directory = [directory stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSURL *url = [NSURL URLWithString:directory];
NSArray *urls = [fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:0 error:&error];


for (int x = 0; x < urls.count; x++) {

NSString *urlIndex = urls[x];


NSString *thing = [urlIndex stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
thing = [thing stringByReplacingOccurrencesOfString:@"file://" withString:@""];


// add thing to new array but crashes before i can
}
}

编辑:异常抛出

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x14ebf1a0'

最佳答案

NSArray *urls = [fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:0 error:&error];

返回一组 NSURL 对象,而不是 NSString。您可以将 URL 转换为字符串简单地用

NSString *path = [urls[x] path];

这使得字符串操作过时了。或者,使用

NSArray *files = [fm contentsOfDirectoryAtPath:directory error:&error];

返回 NSString 对象数组而不是 NSURL

关于ios - 循环遍历 URLS 的 NSArray - 崩溃 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23405392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com