Please tell me why isEqualToString not work ?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
value =[dict objectForKey:#"LANGUAGE"];
if ([value isEqualToString:#"english"])
{
NSLog(#"thx");
}
6,
try using [dict valueForKey:#"LANGUAGE"]; instead of [dict objectForKey:#"LANGUAGE"]; return type are different.
Related
This has become very frustrating over the past day-and-a-bit-too-much: why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
I have used it before, and it worked perfectly: is there something I am forgetting to add in my info.plist? That is the only thing I can think of.
Thank you for helping!
EDIT:
Maybe it has something to do with Xcode 4.5 and iOS6? It worked in 4.3/iOS5
Have you added the .plist file to your project? If so, try this:
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
If not, create the property list in your project.
I write a file to the documents directory of an app:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
[responseString writeToFile:file atomically:YES];
But how can I access the file?
My code looks like this:
NSString *filePath = [[NSFileManager defaultManager] pathForRessorce: #"data" ofType:#"JSON"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
The file path is not populated...can somebody give me a hint?
Thanks!
You are reading from resource folder, not from document directory. You need to populate filePath in the same way that you have used during writing. That is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
"initwithcontentsoffile is deprecated" warning
So Instead of:
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
use:
NSString *userInfoFileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
This gives me...
NSString *filename2 = [NSString stringWithFormat:#"/newFile.json"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", filename2]];
[self.restClient loadFile:filename2 intoPath:getImagePath];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:getImagePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
...this error:
File loaded into path: (null)
...but why?? this code was under view did load. the file is in dropbox, so what is the problem
what is suppose to be wrong here with my code:
-(void)writetofile:(NSMutableString *)str{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/code.txt",path];
NSStringEncoding *encode=NULL;
[str writeToFile:filePath atomically:YES encoding:*encode error:nil];
}
try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:#"code.txt"];
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
You are dereferencing a NULL pointer:
NSStringEncoding *encode = NULL;
`... encoding:*encode ...`
Instead, just use NSUTF8StringEncoding:
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
I'm looking for a way to fill an NSArray with NSStrings that are the names of the directories in the apps documents directory.
This is what I have so far:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (//loop until no more directories)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:path];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
I'm not sure what the for loop's conditions need to be and I'm not sure how to retrieve the directories names.
Thanks for the advice in advance.
Try this
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:urDocumentsFolderPath error:nil] retain];
for(NSString *file in filesArray) {
//Do something
}
So in your case
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil] retain];
for(NSString *file in filesArray)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:file];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
Hope this helps
"apps documents directory" = ~/Users/User/Applications? NSDocumentsDirectory or NSApplicationDirectory?
Maybe:
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileIsDirectory = FALSE;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *content = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; //OSX v10.5
NSEnumerator *enumeratorContent = [content objectEnumerator];
NSString *file;
while ((file = [emumeratorContent nextObject])) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
if (fileManager fileExistAtPath:[documentsDirectory stringByAppendingPathComponent:file] isDirectory:&fileIsDirectory && fileIsDirectory)
[directoriesArray addObject:file];
[pool drain];
Try to use NSDirectoryEnumerator(OSX v.10.0) or NSURL too (OSX v.10.6).