NSString -initWithContentOfFile error - Objective-C -
i'm trying load nsstring
, content of file named names.txt
, in directory of project. this, have following code :
nserror *error = nil; nsstring *names = [[nsstring alloc] initwithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"names" oftype:@"txt"] encoding:nsasciistringencoding error:&error]; if (error != nil) { [nsexception raise:@"error reading file :" format:@"%@",error]; }
and i'm getting *** terminating app due uncaught exception 'error reading file :', reason: 'error domain=nscocoaerrordomain code=258 "the file name invalid."'
. couldn't find other cases that. what's wrong code?
ensure names.txt file included under copy bundle resources region of xcode build phases window target.
in debugger, test valid bundle file with:
... nsstring *file = [[nsbundle mainbundle] pathforresource:@"names" oftype:@"txt"]; /* file ought not nil. nsassert (file) */ nsstring *names = [[nsstring alloc] initwithcontentsoffile: file encoding: nsasciistringencoding error: &error];
Comments
Post a Comment