NSSring from plist created from NSArray - ios

I create a NSArray and write to a file: a.plist.
I use NSString: initWithContentsOfFile, and I can see the content in xml.
Then, I add a.plist to another project
and then I use NSString: initWithContentsOfFile to get the xml string.
filePath = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"plist"];
NSString *plistStr = [[NSString alloc]initWithContentsOfFile:filePath];
However, it failed to recreate the xml string.
I user NSArray to test:
NSArray *plist2Array = [[NSArray alloc]initWithContentsOfFile:filePath];
But it successfully.
I think it may result from "Text Encoding" when I add it to another project.
The problem is I tried UTF8,UTF16 and so on.
I still can't find solution.
Hope for your help,thanks!

I find it!
rename the file: a.plist ->a.txt(or any else),
then in another project you can get the xml string.

Related

Could not able to load Unicode characters from .strings file in cocoa

I am new to Cocoa development. I am using a .strings file to store Key and Value pairs. The value pairs contain some unicode characters like
"\u5f0a\u793e\u306e\u88fd\u54c1\u3092\u8a66\u3057\u3066\u307f\u308b\u3002"
Im using the below statements to parse the strings file
NSString *fname = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"strings"];
NSString *fileText = [NSString stringWithContentsOfFile:fname encoding:NSUTF16StringEncoding error:nil];
NSDictionary *dict= [fileText propertyListFromStringsFileFormat];
NSLog(#"%#",[dict valueForKey:#"key"]);
I am getting ouput without '\' like
"u5f0au793eu306eu88fd"
But I want the output with '\' as exactly in the file.
Please help.

CSV to NSString results in empty null object

I'm trying to retrieve content of a csv file to NSString. Thats what I do:
NSString *strBundle = [[NSBundle mainBundle] pathForResource:#"socs" ofType:#"csv"];
NSLog(#"bundle path: %#",strBundle);
NSString *file = [NSString stringWithContentsOfFile:strBundle
encoding:NSUTF8StringEncoding
error:nil];
if ([[NSFileManager defaultManager]fileExistsAtPath:strBundle]) {
NSLog(#"file is there!!!");
}else {
NSLog(#"no file");
}
NSLog(#"file: %#",file);
NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSLog(#"lines: %lu",(unsigned long)[allLines count]);
file manager shows that the file is there. When i try to log the NSString or number of files it says null. I even created NSData object with the content of exactly the same file and when I logged the NSData object, I clearly saw that there is some data. Then when I tried to create NSString with the content of NSData, I had the same result as before - null. Maybe the problem is somewhere in the formatting of the file?
Any help will be appreciated :)
I see 3 issues:
You are passing a nil argument to the error: parameter in your stringWithContentsOfFile: line. If there's a possibility something might go wrong (and apparently there is), you should pass a real argument there so you can figure out what went wrong.
You can use componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet], but that has a tendency to produce blank "components" between every line. Plain old #"\n" works better in virtually all cases I've run into.
You should be checking fileExistsAtPath before you try to load it into the NSString
If you were truly able to create an NSData object from the path it doesn't necessarily mean it's correct data. But let's say it is, if you were not able to convert it to a NSString then you need to check your encoding parameter.

Read a plist from website

I am incorporating an image gallery into my project. It currently reads from a local plist file, and I need to change it to read from a web server. I've read a number of ways to do this, but can't make it work in my context. This seems maddeningly simple.
Below is my code:
NSString *plistPath =
[[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
You can view my plist here
This is what I've tried to no avail:
//get image URLs
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSDictionary *imagePaths = [[NSDictionary alloc] initWithContentsOfURL:plistPath];
I'd greatly appreciate anyone's help.
Try this and it should be work :
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfURL:plistPath];
NSLog(#"%#", imagePaths);
The URL you provide serves a plist that contains an array.
Your code tries to read it as a dictionary.
Just change your code to read it as an array and you'll be one step closer.
But as others have said in the comments, you can't download a file (plist or otherwise) to your bundles directory. You have to download it to your Documents directory or similar.

Objective-C: Reading from a .rtf file into an array

I've looked around a bit and tried a few things, but none have really worked. What I'm trying to do is create a NSArray of NSStrings, with each array value corresponding to one line from the Rich Text File I'm referencing. At first I tried this:
NSArray* data = [[NSString stringWithContentsOfFile:#"relevantFile.rtf" encoding:4 error:nil] componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
I've also tried this, because I found it in the iOS developer library:
NSArray* data = [[NSArray alloc] initWithContentsOfFile:#"relevantFile.rtf"];
However, neither of these has worked for me. A few lines later in the code, in order to diagnose errors, I have the following code:
for(int i = 0; i < [data count]; i++)
{
NSLog(#"%#", [data objectAtIndex: i]);
}
...for which NSLog is printing "(null)". I'm not entirely sure what I'm doing wrong here -- should I be using mutable strings or arrays, or is there some better way to go about this that I don't know about?
That first line you posted should do it. My guess would be that it's not finding the file. Not specifying an absolute path, the app will look in the current directory which is probably NOT where the file is.
If the file is a resource that is compiled into your app bundle, you can use the following code to obtain the path to it:
NSString* path = [[NSBundle mainBundle] pathForResource: #"relevantFile" ofType: #"rtf"]
NSArray* data = [[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

Get and update plist value

I created a plist titled "appData.plist" in my supporting files section of Xcode. I have a key called "keyone" and am trying to get the value of it. I'm trying to log the value to console with NSLog but its not working.
How do you do this? I've searched google and there are a lot of way but none work for me.
NSString *myFile = [[NSBundle mainBundle] pathForResource: #"appData" ofType: #"plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:myFile];
NSString *viewCover = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"keyone"];
This is what i've tried but the whole thing ends in errors, I can't log it and I get an 'unusable variable' error.
The objectForInfoDictionaryKey method retrieves a value from your app's Info.plist file, which isn't what you want -- you want to retreive it from your dictionary myDict. You mentioned that the value is actually a boolean, so replace the last line with
BOOL viewCover = [[myDict objectForKey:#"keyOne"] boolValue];

Resources