I am trying to execute the code below to find out if I can get it to extend the absolute path for these locations so that I can use them for actions with NSFileManager that fail when I use the tilde and relative paths.
I am working on a command line application in Xcode in Objective-C. I can run the program from the command line, and it extends the path for me, but from the target in Xcode I pass the values ββfor the command line arguments using $ PROJECT_DIR and $ HOME to get part of the path from me. The trouble is that I need to get to $ PROJECT_DIR / .. which is not allowed using NSFilemanager.
It does not look like URLByResolvingSymlinksInPath or URLByStandardizingPath are working as I expect. Is there anything else I should do?
BOOL isDir = TRUE; for (NSString *path in @[@"~/", @".", @".."]) { NSURL *url = [[[NSURL URLWithString:path] URLByResolvingSymlinksInPath] URLByStandardizingPath]; DebugLog(@"path: %@", url.absoluteString); DebugLog(@"Exists: %@", [[NSFileManager defaultManager] fileExistsAtPath:url.path isDirectory:&isDir] ? @"YES" : @"NO"); }
Update: I use the realpath from stdlib to resolve the path and created the following method, although I do not understand this C function. In particular, I do not know how much the value is resolved or how I will use it. I see to get the expected return value.
- (NSString *)resolvePath:(NSString *)path { NSString *expandedPath = [[path stringByExpandingTildeInPath] stringByStandardizingPath]; const char *cpath = [expandedPath cStringUsingEncoding:NSUTF8StringEncoding]; char *resolved = NULL; char *returnValue = realpath(cpath, resolved);
source share