what is the default saving location of NSCoding protocol?
Also, is there any way to change such a default location to say, the folder where the .app file is located?
You can directly write your encoded object data encoded according NSCoding to a file using NSKeyedArchiver
Like this:
BOOL result = [NSKeyedArchiver archiveRootObject:yourObject toFile:filename];
With filename you can choose your file location (you may set it to the documents directory if you are on iOS).
EDIT 1:
If you'd like to store in into NSUserDefault... do:
NSData *yourObjectAsNSData = [NSKeyedArchiver archivedDataWithRootObject:yourObject];
[[NSUserDefaults standardUserDefaults] setObject:yourObjectAsNSData forKey:#"aKey"]
Related
I have a browse function where I get the url of a file.
Now I want to save the file in the supporting file dictionary so that if the file is move anywhere else it can still access it
I have a code which saves it to the supporting files:
NSURL *mainUrl;
mainUrl=[[NSBundle mainBundle] bundleURL];
NSFileManager *Fm;
[Fm copyItemAtURL:url toURL:mainUrl error:nil];
but I don't know what the name and the extension because the browse function allows png,jpg,jepg files
and I would need the name to access it
so my question would be how I can I save the file there with a name and extension of my choose
my name would look like that:
NSString *string;
NSInteger number;
number=0;
string=[NSString stringWithFormat:#"%#%li",#"img",(long)number];
and the extension would be jpg
can somebody help me?
You can use NSString's -stringByAppendingPathExtension: method:
[#"foo" stringByAppendingPathExtension: #"jpg"];
results in #"foo.jpg".
You could get the file name and the extension using the specified full path and the following functions.
/* NSString Class References*/
lastPathComponent
Returns the last path component of the receiver.
(NSString *)lastPathComponent
pathExtension
Interprets the receiver as a path and returns the receiver’s extension, if any.
(NSString *)pathExtension
In my application(iPad application) I have 5 folders and inside of each folder i have one XML file. My question is, How can I call all .xml files, in my appDelegate
I have file1.xml, file2.xml, file3.xml, file4.xml,file5.xml (it's a requirement)
for call or adding Path for file1 xml I have this code:
NSString *xml = [[NSBundle mainBundle] pathForResource:#"file1" ofType:#"xml"];
NSData *Data = [NSData dataWithContentsOfFile:xml];
NSXMLParser *Parser = [[NSXMLParser alloc] initWithData:Data];
file1 *parser = [[file1 alloc] initXMLParser];
but how can I have all 5 in my appDelegate class?
and do I need to create specific parser class for each or since all information and tags are the same I just need to add all in my appDelegate
EDIT:
I have to call them from their folder I cann"t change the structure for example
Folder1/file1.xml
Folder2/file2.xml
Folder3/file3.xml and so on
Yes, you need to instantiate an NSXMLParser object for each XML file you're parsing. The simple way to load the XML files is as kimsnarf says: use a for loop and load them in order. If they're in the bundle (which they appear to be), I'd stick them in a specific path under "Resources," like "Resources/SpecialXMLJunk" and just load them by iterating over the results of something like URLsForResourcesWithExtension:subdirectory: (used to get the XML files out of "Resources/SpecialXMLJunk"). So, pseudocode-ish, probably something like this:
NSArray *xmlResourceURLs = [mainBundle
URLsForResourcesWithExtension: #"xml"
subdirectory: xmlResourcesPath];
foreach (NSURL *xmlURL in xmlResourceURLs)
[self loadJunkXMLAtURL: xmlURL];
Create a for-loop and load/parse the files one by one. You should store the parsed data somewhere anyway (in a cache or database) so you don't need to hold on to the files and parsers after parsing. Retrieve data from the cache/database instead.
I am trying to store an NSMutableArray of images using persistive storage but it is not storing it
here is my code
[imagingList addObject:image ]; //image is of type UIImage
imagingHistory=[NSUserDefaults standardUserDefaults];
[imagingHistory setObject:imagingList forKey:#"imaging history" ];
and in my view did load I write
imagingList = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:#"imaging history" ];
when I re-load the application the list of objects is set back to Zero
am I missing something ?
You can't save a UIImage directly in the user defaults. You have to convert it to an NSData first. Just by chance, when I was looking for the NSUserDefaults reference page to answer this question, a found a tutorial on how to do exactly what you want.
Hmm you really shouldn't be trying to save UIImages into NSUserDefaults. Take a look at this resource https://discussions.apple.com/thread/1729849?start=0&tstart=0 it explains how to get the NSData of the image and write it to a file. From there you can save a reference (NSURL or NSString) to the image's path in your app's document directory and you can load it from there using something like UIImage aImage = [UIImage imageWithData:[NSData datWithContentsOfURL:pathURL]].
Hope that helps.
Ok I can get the list of IPA files in my iTunes folder. what I want to do is be able to read the plist file in the IPA file.
I'm very new to Objective-C so sorry if this is an obvious question (I did look).
I tried the following but it comes back null.
// i = NSURL. value eg. "file://localhost/Users/jiyeon/Desktop/test/test.ipa"
NSDictionary *plistInfo = [NSDictionary dictionaryWithContentsOfFile:[[i absoluteString]
stringByAppendingPathComponent:#"iTunesMetadata.plist"]];
However plistInfo just ends up being null.
Try -(NSString *)path NSURL
NSDictionary *plistInfo = [NSDictionary dictionaryWithContentsOfFile:[[i path]
stringByAppendingPathComponent:#"iTunesMetadata.plist"]];
this should work
Is there anyway to do Files Handling in Objective-C? I am just trying to do simple read and write and can use 'c' but i am force to use Objective-C classes for that :#. I am looking into NSInputStream, but its going over my head. Is there any tutorial which explains how to use NSInputStream?
I had trouble with basic file i/o when I first hit it in Obj-C as well. I ended up using NSFileHandle to get C style access to my file. Here's a basic example:
// note: myFilename is an NSString containing the full path to the file
// create the file
NSFileManager *fManager = [[NSFileManager alloc] init];
if ([fManager createFileAtPath:myFilename contents:nil attributes:nil] != YES) {
NSLog(#"Failed to create file: %#", myFilename);
}
[fManager release]; fManager = nil;
// open the file for updating
NSFileHandle *myFile = [NSFileHandle fileHandleForUpdatingAtPath:myFilename];
if (myFile == nil) {
NSLog(#"Failed to open file for updating: %#", myFilename);
}
// truncate the file so it is guaranteed to be empty
[myFile truncateFileAtOffset:0];
// note: rawData is an NSData object
// write data to a file
[myFile writeData:rawData];
// close the file handle
[myFile closeFile]; myFile = nil;
If all you need to do is really simple I/O, you can just tell an object to initialize itself from, or write itself to, a filesystem path or URL. This works with several Foundation classes, including NSString, NSData, NSArray, and NSDictionary among others.
Try starting out by looking at the following two NSString methods:
- initWithContentsOfFile:encoding:error:
- writeToFile:atomically:encoding:error:
I find apple's guides short and to the point.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html