I am trying to save an array, which has some dictionaries inside, to a plist file but it fails. I don't get any errors. I do exactly the same few lines above in the code just with another array and that works.. I can't figure out why it does not save the file.
This is where I save the file: (see some debugger output below)
// When built parse through dictionary and save to file
for ( NSString *keys in [dicByCountry allKeys] )
{
NSArray *arrr = [[NSArray alloc] initWithArray:[dicByCountry objectForKey:keys]];
NSString *fname = [self filePath:[NSString stringWithFormat:#"regions.cid%#.plist",keys]];
if (![arrr writeToFile:fname atomically:YES])
NSLog(#"Could not write file regions.cid%#.plist",keys);
}
Here some GDB Output
(gdb) po fname
/Users/chris/Library/Application Support/iPhone Simulator/4.0/Applications/44A9FF9E-5715-4BF0-9BE2-525883281420/Documents/regions.cid0.plist
(gdb) po arrr
<__NSArrayI 0x8022b30>(
{
countryID = "<null>";
region = "?\U00e2vora";
regionID = 16;
},
{
countryID = "<null>";
region = Vicenza;
regionID = 14;
},
{
countryID = "<null>";
region = Wales;
regionID = 23;
}
)
If you read the documentation closely, writeToFile:atomically: expects the array to contain only objects which can be written into a plist file.
Only objects of type:
NSString
NSData
NSDate
NSNumber
NSArray
NSDictionary
are permitted. If you have arrays or dictionaries within the array you're saving, their values will be examined by the same criteria.
This is somewhat more restrictive than what's usually allowed in NSArrays. In particular, the value [NSNull null] is not acceptable.
I convert NSArray or NSDictionary to NSData before serializing. Following is a category on nsarray for serializing and deserializing. This comfortableby handles some data being nsnull
#implementation NSArray(Plist)
-(BOOL)writeToPlistFile:(NSString*)filename{
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
return didWriteSuccessfull;
}
+(NSArray*)readFromPlistFile:(NSString*)filename{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
NSData * data = [NSData dataWithContentsOfFile:path];
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
#end //needs to be set for implementation
Related
I am working on storing a list of audio files into my document directory and then fetching them.
It gives me a list of audio files along with this it gives me a file with name #".DS_Store". While fetching content I want to leave this file of documents directory.
Is there any way I can get rid of this while fetching the audio list other than removing this from array or apply a #".DS_Store" check.
What exactly is the reason for this.?
#pragma mark - Saving Audio in Document Directory
-(void)saveAudioinDocumentDirectory:(ASIHTTPRequest *)theRequest
{
/*save the Audio file in Document Directory */
NSFileManager *fileManager=[NSFileManager defaultManager];
NSLog(#"GOT THE SIZe OF AUDIO %d",[[theRequest responseData] length]);
NSLog(#"AUDIO ID IS %#",[[theRequest userInfo] valueForKey:#"audioIndex"]);
/*Get the Path to Application Documents Directory*/
NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/*append the neccessary file extension */
NSString *filepathStr=[NSString stringWithFormat:#"/%#/%#.mp3",docDir,[NSString stringWithFormat:#"%#",[[theRequest userInfo] valueForKey:#"audioIndex"]]];
/*Check if my crrent file exists in the Documents Directory*/
if(![fileManager fileExistsAtPath:filepathStr])
{
/* file doesnt exists */
/*create a NSdata of File*/
NSData *data=[NSData dataWithData:[theRequest responseData]];
NSLog(#"%#",filepathStr);
if ([data length] >0 ){
/*write the File at the Location in Documents Directory */
[data writeToFile:filepathStr atomically:YES];
NSLog(#"Successfully saved the file to %#", filepathStr);
}
else if([data length] == 0)
{
NSLog(#"Nothing was downloaded.");
}
}
/*After saving fetch the path til documents Directory*/
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
/*Get the Path for Files */
NSString *s=[folders objectAtIndex:0];
/*Fetch the list of Files stored in Documents Directory*/
NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
NSLog(#"TOTAL NUMBER OF AUDIO FILES %d %#",[contents count],[contents objectAtIndex:0]);
if([Audiolistforstr isEqualToString:#"AudioListForIntro"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio from document Intro IS %#",Audiolistforstr);
/*Intro*/
[AudioListArrForIntro removeAllObjects];
[AudioListArrForIntro addObjectsFromArray:contents];
if([AudioListArrForIntro containsObject:#".DS_Store"])
{
[AudioListArrForIntro removeObject:#".DS_Store"];
}
NSLog(#"FINAL LIST %#",AudioListArrForIntro);
}
else if([Audiolistforstr isEqualToString:#"AudioListForCredits"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio from document credit IS %#",Audiolistforstr);
/*credits*/
[AudioListArrForCredits removeAllObjects];
[AudioListArrForCredits addObjectsFromArray:contents];
if([AudioListArrForCredits containsObject:#".DS_Store"])
{
[AudioListArrForCredits removeObject:#".DS_Store"];
}
NSLog(#"FINAL LIST %#",AudioListArrForCredits);
}
/* Did we find anything? */
if([Audiolistforstr isEqualToString:#"AudioListForIntro"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio fromRELOADNG TABLE Intro IS %#",Audiolistforstr);
/*Intro*/
if ([AudioListArrForIntro count] == 0)
{
}
else
{
UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioIntroTag];
[(UITableView *)[vw viewWithTag:tblIntroAudioListTag] reloadData];
}
}
else if([Audiolistforstr isEqualToString:#"AudioListForCredits"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio fromRELOADNG TABLE Intro IS %#",Audiolistforstr);
/*Credits*/
if ([AudioListArrForCredits count] == 0)
{
}
else
{
/*AudioListForCredits*/
UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioCreditsTag];
[(UITableView *)[vw viewWithTag:tblCreditsAudioListTag] reloadData];
}
}
}
Any help would be appreciated.
Thanks
Vikas
You can check for .DS_Store after NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; line........that is,
NSMutableArray * dirContents = [[NSMutableArray alloc] initWithArray:docDir];
if([docDir containsObject:#".DS_Store"])
{
[dirContents removeObject:#".DS_Store"];
}
By this, dirContents removes the entry of .DS_Store.
Filter your document directory contents. For example, if you are having audio files with extension of .mp3, then you can get all the mp3 files as below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
directoryContent = [directoryContent filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"pathExtension ==[c] %#", #"mp3"]];
This will omit all other files than the mp3 files..
All the best!!!
The API you're using:
NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
returns all files found at the path, which would include the ".DS_Store" file.
I'd recommend assigning "contents" to a mutable array, e.g.:
NSMutableArray * contents =
[[NSMutableArray alloc] initWithArray: [fileManager contentsOfDirectoryAtPath:s error:NULL]];`
and iterate through the array to find and removing any and all files that don't have ".mp3" as a path extension.
I'd also recommend not starting any variable with an upper case letter (e.g. instead of "Audiolistforstr", use "audiolistforstr" or even better, "arrayofAudioFiles"). Objective C best practice is to start all variables and methods with lower case letters.
Its Working well..
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (int i = 0; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:#"%#/%#",documentsDirectory,[imageFilenames objectAtIndex:i] ];
if (![[imageFilenames objectAtIndex:i]isEqualToString:#".DS_Store"])
{
UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
}
}
I managed to successfully save data to a plist (in this case, a bookmark list), but I can't seem to figure out how to prevent the user from saving same data twice. I'm using "moveRowAtIndexPath" to re-order the bookmarks. When there is a duplicate on the list, it causes a crash during sorting. Here is my code:
- (IBAction)addBookmarkButtonClicked:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Bookmarks.plist"];
NSMutableArray *bookmarksArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == bookmarksArray) {
bookmarksArray = [[NSMutableArray alloc] initWithCapacity:0];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:gameName.text.self forKey:#"gameName"];
[array setObject:gameRating.text.self forKey:#"gameRating"];
[bookmarksArray addObject:array];
[bookmarksArray writeToFile:plistPath atomically: TRUE];
}
Before you add the item to the BookmarksArray you should check if it already exists.
You can do that by looping thorough all the items in the BookmarksArray and compare each item's keys to the new value you're trying to add. Example:
BOOL itemExist, sameName, sameRating;
itemExist=NO;
sameName=NO;
sameRating=NO;
For (int i=0, i<[bookmarksArray count], i++) {
If ([gameName.text isEqualToString:[[bookmarksArray itemAtIndex:i] itemForKey:gameName]]) {
sameName=YES;
}
If ([gameRating.text isEqualToString:[[bookmarksArray itemAtIndex:i] itemForKey:gameRating]]) {
sameRating=YES;
}
if (sameName && sameRating) itemExist=YES;
sameName=NO;
sameRating=NO;
}
if (!itemExist) {
//item can be added
}
I want to read the text from the localizable.strings file. I am collecting the strings for translation from several directories and file in one .strings file. But then I have several copies of the same translation strings. I want to remove this programmatically.
So I need to read the strings only (not the comments) from the .strings file, and
- then sort them,
- remove repeated strings
then create a new .strings file.
Is it possible to read the strings file and keep the key string and translated value in a dictionary. I mean any built-in method to read a .text file, only the "key " = "value" part, avoiding /* ... */ or # comments part. Like reading a config file.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Localizable"
ofType:#"strings"
inDirectory:nil
forLocalization:#"ja"];
// compiled .strings file becomes a "binary property list"
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *jaTranslation = [dict objectForKey:#"hello"];
I am happy to find an excellent API in NSString class. The code is below.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Localizable" ofType:#"strings"];
NSString *fileText = [NSString stringWithContentsOfFile:filePath encoding: NSUnicodeStringEncoding error:nil];
NSDictionary *stringDictionary = [fileText propertyListFromStringsFileFormat];
NSArray *allKeys = [stringDictionary allKeys];
NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSString *documentsDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
documentsDirectory = [paths objectAtIndex:0];
}
NSString *outputPath = [documentsDirectory stringByAppendingString:#"/Localizable.strings"];
NSLog(#"Strings contents are writing to: %#",outputPath);
[[NSFileManager defaultManager] createFileAtPath:outputPath contents:nil attributes:nil];
NSFileHandle *outputHandle = [NSFileHandle fileHandleForWritingAtPath:outputPath];
[outputHandle seekToEndOfFile];
for(id key in sortedKeys){
NSString *line = [NSString stringWithFormat:#"\"%#\" = \"%#\";\n", key, [stringDictionary objectForKey:key]];
NSLog(#"%#",line);
[outputHandle writeData:[line dataUsingEncoding:NSUnicodeStringEncoding]];
}
}
Solution in Swift
I created a swift code following the Objective-C code answered by #jason-moore above
This solution is swift equivalent.
guard let path = Bundle.main.url(forResource: "Localizable", withExtension: "strings", subdirectory: nil, localization: "ja") else {
return nil
}
guard let dict = NSDictionary(contentsOf: path) else {
return nil
}
guard let jaTranslation = dict.value(forKey: "hello") as? String else {
return nil
}
What I was looking for is a way to read a json from a bundle file into a NSDictionary and then print it inside a UITextView.
The result was not nicely formatted!
I used a part of Karim's answer from above to create a method that generated a beautified json in a string:
-(void)setText:(NSDictionary *)json
{
NSArray *allKeys = [json allKeys];
_beautyStr = #"";
for(id key in allKeys){
NSString *line = [NSString stringWithFormat:#"\"%#\" = \"%#\";\n", key, [text objectForKey:key]];
_beautyStr = [NSString stringWithFormat:#"%#%#",_beautyStr, line];
}
NSLog(#"%#",_beautyStr);
}
I have written a class to help save and load data for the sake of persistence for my iPhone application but I have a problem with some NSUIntegers that I'm passing across.
Basically, I have the code to use pointers, but eventually it has to start out being an actual value right? So I get this error
warning: passing argument 1 of 'getSaveWithCampaign:andLevel:' makes pointer from integer without a cast
My code is laid out like so.
(Persistence is the name of the class)
NSDictionary *saveData = [Persistence getSaveWithCampaign:currentCampaign andLevel:[indexPath row]];
Here's Persistence.m
#import "Persistence.h"
#implementation Persistence
+ (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kSaveFilename];
}
+ (NSDictionary *)getSaveWithCampaign:(NSUInteger *)campaign andLevel:(NSUInteger *)level
{
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSDictionary *saveData = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSString *campaignAndLevelKey = [self makeCampaign:campaign andLevelKey:level];
NSDictionary *campaignAndLevelData = [saveData objectForKey:campaignAndLevelKey];
[saveData release];
return campaignAndLevelData;
}
else
{
return nil;
}
}
+ (NSString *)makeCampaign:(NSUInteger *)campaign andLevelKey:(NSUInteger *)level
{
return [[NSString stringWithFormat:#"%d - ", campaign+1] stringByAppendingString:[NSString stringWithFormat:#"%d", level+1]];
}
#end
You should get rid of the * in ( NSUInteger *) in the getSaveWithCampaign method.
If you read the error message closely, it states that you are making a pointer (*) from an integer without a cast.
Your getSaveWithCampaign method should now have the following signature:
+ (NSDictionary *)getSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level
If, on the other hand, for some reason you do want to use pointers, you can pass in the NSUInteger prefixed with an ampersand (&) to pass in the address of the NSUInteger in memory.
I'm saving some data using a series of NSDictionaries, stored in an NSMutableArray and archived using NSKeyedArchiver.
I'm basically trying to save the states of several instances the class 'Brick', so I've implemented a getBlueprint method like this (slimmed down version)
-(id)getBlueprint
{
// NOTE: brickColor is a string
NSDictionary *blueprint = [NSDictionary dictionaryWithObjectsAndKeys:
brickColor, #"color",
[NSNumber numberWithInt:rotation], #"rotation",
nil];
return blueprint;
}
And so I have another method that creates a new Brick instance when provided with a blueprint.
-(id)initWithBlueprint:(NSDictionary *)blueprint spriteSheet:(NSString *)ssheet
{
if((self == [super init])){
brickColor = [blueprint objectForKey:#"color"];
[self setColorOffset:brickColor];
while(rotation != [[blueprint objectForKey:#"rotation"] intValue]){
[self setRotation:90];
}
}
return self;
}
Which works when I pass it a 'fresh' blueprint, but not when I read a blueprint from a saved file... sort of. For example, the rotation will work, but changing the color wont. So while I can read the value of brickColor using
NSLog(#"brick color %#", [blueprint objectForKey:#"color"]);
if I try something like
if(brickColor == #"purple"){
colorOffset = CGPointMake(72,36);
NSLog(#"Changed offset for -- %# -- to %#", color, NSStringFromCGPoint(colorOffset));
}
And I know that color is purple, the condition doesn't return true. I thought it might be that somehow NSKeyedUnarchiver changed a string into something else, but the following test returns true.
if([color isKindOfClass:[NSString class]]){
NSLog(#"%# IS A STRING", color);
}else{
NSLog(#"!!!!! COLOR IS A NOT STRING !!!!!");
}
As I said, this isn't a problem if I try to use a freshly created NSDictionary as a blueprint, only when a blueprint is archived and then read back in.
So, as usual, I'm wondering if anyone has any ideas why this might be happening.
incase it's relevant, here's how the data is being stored and recieved.
// Saving
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void)buildLevelData{
levelData = [[NSMutableArray alloc] initWithCapacity:100];
for(brickSprite *brick in spriteHolder.children){
[levelData addObject:[brick getBlueprint]];
}
}
-(void)saveLevel
{
[self buildLevelData];
NSData *rawDat = [NSKeyedArchiver archivedDataWithRootObject:levelData];
if([self writeApplicationData:rawDat toFile:saveFileName]){
NSLog(#"Data Saved");
}else{
NSLog(#"ERROR SAVING LEVEL DATA!");
}
[[Director sharedDirector] replaceScene:[MainMenu scene]];
}
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(#"Documents directory not found!");
return NO;
}
NSString *appFile = [saveDir stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
// Loading
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- (void) loadRandomMapFrom:(NSString *)dir
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
if(!docsDir){
NSLog(#"Cound Not Find Documents Directory When trying To Load Random Map");
return;
}
dir = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#", dir]];
// we'll also set the file name here.
NSArray *existingFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:nil];
// get last file for this test
NSString *filePath = [dir stringByAppendingPathComponent:[existingFiles objectAtIndex:([existingFiles count] - 1)]];
NSMutableArray *levelData = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[self buildMapWithData:levelData];
}
-(void)buildMapWithData:(NSMutableArray *)lData
{
for(NSDictionary *blueprint in lData){
brickSprite *brick = [[brickSprite alloc] initWithBlueprint:blueprint spriteSheet:#"blocks.png"];
[spriteHolder addChild:brick];
}
}
Sorry about the mess of a question. There's a lot going on that I'm struggling to fully understand myself so it's hard to break it down to the bare minimum.
You should always compare strings with [firstString isEqualToString:secondString], because firstString == secondString only checks for pointer equality, e.g. if both strings are stored at the same location (which they'll never be when comparing dynamically created objects and string constants).