我來到這個https://github.com/karelia/KSFileUtilities,但它不適用於文件URL。所以我寫了這個。 http://pastebin.com/FKYjVwpU
#import <Foundation/Foundation.h>
NSString * relativize(NSURL * to, NSURL * from, BOOL fromIsDir);
int main(int argc, const char * argv[])
{
@autoreleasepool {
//NSURL * u = [NSURL URLWithString:@"../../scripts" relativeToURL:[NSURL fileURLWithPath:@"/Users/aaronsmith/Development/aaron/"]];
//NSLog(@"%@",u.absoluteString);
//NSLog(@"%@",[NSURL URLWithString:@"../../scripts.py" relativeToURL:[NSURL fileURLWithPath:@"/Users/aaronsmith/scipts/"]]);
NSURL * to = NULL;
NSURL * from = NULL;
to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/upload-issue.py"];
from = [NSURL fileURLWithPath:@"/Users/aaronsmith/scripts.scrpt"];
assert([relativize(to,from,FALSE) isEqualToString:@"./uverse/upload-issue.py"]);
to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/upload-issue.py"];
from = [NSURL fileURLWithPath:@"/Users/aaronsmith/"];
assert([relativize(to, from, TRUE) isEqualToString:@"./uverse/test/upload-issue.py"]);
relativize(to,from,TRUE);
to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/upload-issue.py"];
from = [NSURL fileURLWithPath:@"/Users/aaronsmith/example/scripts/"];
assert([relativize(to,from,TRUE) isEqualToString:@"../../uverse/test/upload-issue.py"]);
to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/scripts/stuff/upload-issue.py"];
from = [NSURL fileURLWithPath:@"/Users/johnson/scripts/"];
assert([relativize(to,from,TRUE) isEqualToString:@"../../aaronsmith/uverse/test/scripts/stuff/upload-issue.py"]);
to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/"];
assert([relativize(to,from,TRUE) isEqualToString:@"./upload-issue.py"]);
to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/scripts.scrpt"];
assert([relativize(to,from,FALSE) isEqualToString:@"./upload-issue.py"]);
to = [NSURL URLWithString: @"/Users/aaronsmith/uverse/test/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/test/whatever/scripts/script.scrpt"];
assert([relativize(to,from,FALSE) isEqualToString:@"../../../uverse/test/upload-issue.py"]);
to = [NSURL URLWithString: @"/Users/aaronsmith/uverse/test/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/test/whatever/scripts/"];
assert([relativize(to,from,TRUE) isEqualToString:@"../../../uverse/test/upload-issue.py"]);
to = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/scripts.scrpt"];
assert([relativize(to, from, FALSE) isEqualToString:@"./upload-issue.py"]);
to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"];
from = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/scripts.scrpt"];
assert([relativize(to, from, FALSE) isEqualToString:@"../../upload-issue.py"]);
}
return 0;
}
NSString * relativize(NSURL * to, NSURL * from, BOOL fromIsDir) {
NSString * toString = [[to absoluteString] stringByStandardizingPath];
NSMutableArray * toPieces = [NSMutableArray arrayWithArray:[toString pathComponents]];
NSString * fromString = [[from absoluteString] stringByStandardizingPath];
NSMutableArray * fromPieces = [NSMutableArray arrayWithArray:[fromString pathComponents]];
NSMutableString * relPath = [NSMutableString string];
NSString * toTrimmed = toString;
NSString * toPiece = NULL;
NSString * fromTrimmed = fromString;
NSString * fromPiece = NULL;
NSMutableArray * parents = [NSMutableArray array];
NSMutableArray * pieces = [NSMutableArray array];
if(toPieces.count >= fromPieces.count) {
NSUInteger toCount = toPieces.count;
while(toCount > fromPieces.count) {
toPiece = [toTrimmed lastPathComponent];
toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
[pieces insertObject:toPiece atIndex:0];
toCount--;
}
while(![fromTrimmed isEqualToString:toTrimmed]) {
toPiece = [toTrimmed lastPathComponent];
toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
fromPiece = [fromTrimmed lastPathComponent];
fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
if(![toPiece isEqualToString:fromPiece]) {
if(![fromPiece isEqualToString:[fromPiece lastPathComponent]] || fromIsDir) {
[parents addObject:@".."];
}
[pieces insertObject:toPiece atIndex:0];
}
}
} else {
NSUInteger fromCount = fromPieces.count;
while(fromCount > toPieces.count) {
fromPiece = [fromTrimmed lastPathComponent];
fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
if(![fromPiece isEqualToString:[fromString lastPathComponent]] || fromIsDir) {
[parents addObject:@".."];
}
fromCount--;
}
while(![toTrimmed isEqualToString:fromTrimmed]) {
toPiece = [toTrimmed lastPathComponent];
toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
fromPiece = [fromTrimmed lastPathComponent];
fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
[parents addObject:@".."];
[pieces insertObject:toPiece atIndex:0];
}
}
[relPath appendString:[parents componentsJoinedByString:@"/"]];
if(parents.count > 0) [relPath appendString:@"/"];
else [relPath appendString:@"./"];
[relPath appendString:[pieces componentsJoinedByString:@"/"]];
NSLog(@"%@",relPath);
return relPath;
}
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString :relativeToURL: – Rakesh
當然,我看過文檔。 NSURL沒有我要找的第一部分。 – gngrwzrd