Bạn có thể lấy URL của tập tin trong gói chính sử dụng
NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];
Bạn có thể viết URL này, ví dụ, một tập tin danh sách tài sản trong các tài liệu thư mục:
NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:@"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:@"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];
Nếu bạn không biết tên của các tập tin và bạn chỉ muốn liệt kê tất cả các tập tin trong gói, sử dụng
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// do something with `url`
}
Nguồn
2012-08-30 21:34:24
"Tham chiếu tệp" là gì? Con đường? URL? Gì? –
Bằng cách tham khảo tập tin tôi có nghĩa là URL tập tin –