objective c - NSURL path not working, but NSString path working -
i saved object file , trying run check on whether or not file exists. have confirmed path of file , concluded if statement works when hard code path nsstring, see first block. however, when seek saving path nsurl, , convert nsstring filemanager can run it's method on it, not locate file @ path. missing here?
locates file here using hard code nsstring:
[nskeyedarchiver archiverootobject:employees tofile:@"/users/xxx/documents/employees.plist"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsstring *employeespath = @"/users/xxx/documents/employees.plist"; if ([filemanager fileexistsatpath:employeespath]) { nslog(@"it exists! yes!"); } else { nslog(@"doesn't exist, sorry bud"); }
does not locate file using nsurl:
[nskeyedarchiver archiverootobject:employees tofile:@"/users/xxx/documents/employees.plist"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsurl *employeespath = [nsurl fileurlwithpath:@"/users/xxx/documents/employees.plist"]; nsstring *employeesstring = [employeespath absolutestring]; if ([filemanager fileexistsatpath:employeesstring]) { nslog(@"it exists! yes!"); } else { nslog(@"doesn't exist, sorry bud"); }
edit
-- if wanted utilize nsurl method, making function store path nsstring proper way. ended working:
nsstring* getpropertylistpath() { nsurl *documentdir = [[nsfilemanager defaultmanager] urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask appropriateforurl:nil create:no error:nil]; nsurl *plist = [documentdir urlbyappendingpathcomponent:@"employees.plist"]; homecoming plist.path;
}
don't utilize nsurl
file path intermediary.
nsurl *employeespath = [nsurl fileurlwithpath:@"/users/xxx/documents/employees.plist"]; nsstring *employeesstring = [employeespath absolutestring]; nslog(@"employeesstring: %@", 'employeesstring');
output:
employeesstring: 'file:///users/xxx/documents/employees.plist'
which not file path.
objective-c nsstring nsurl nsfilemanager
No comments:
Post a Comment