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

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.

Related

How can I read excel file by using objective-c iOS?

I need to pull out the excel file to interface that user can only read the information in file.
what is the solution to implement it?
The most easy way to solve this is first, convert the Excel file to CSV format, which stands for Comma Seperated Value. Meaning it's formatted like: cell 1,cell 2,cell 3. And a new line for each row.
The second is to read the file into a String which can be done in two ways, depending if you have it local or not. Let's say you have it on a server.
NSURL *url = [NSURL urlWithString:#"http://urltoyour.excel/file.csv"];
NSData *data = [NSData dataWithContentOfURL:url];
NSString *string = [NSString stringWithData:data];
Then you can easily convert this to arrays using
NSArray *lines = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
Now you have each excel line in that array. Now for each line you probably want the columns in an array too, you can do that using:
NSMutableArray *finalArray = [NSMutableArray new];
for (NSString *line in lines) {
NSArray *array = [line componentsSeperatedByString:#","];
[finalArray addObject:array];
}
Good luck and let me know if that works out for you!

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.

iOS: read raw plist

I want to read the UserDefaults plist but not as Dictionary or Data. I want it as string like it is when you open it with an editor.
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *homeDir = [documentsPath stringByReplacingOccurrencesOfString:#"Documents" withString:#""];
NSString *defaultsPath = [homeDir stringByAppendingString:[NSString stringWithFormat:#"Library/Preferences/%#.plist", [[NSBundle mainBundle] bundleIdentifier]]];
NSData *data = [NSData dataWithContentsOfFile:defaultsPath];
Already tried:
`NSString *contents = [NSString stringWithContentsOfFile:defaultsPath encoding:NSUTF8StringEncoding error:&error];
which ends up with
The operation couldn’t be completed. (Cocoa error 261.)
Property list formats can be either binary or text. Binary plists can't be loaded into an NSString because strings are for text, not arbitrary binary data. The error you're getting seems to suggest that the file cannot be interpreted as UTF-8, which either means it is encoded using another encoding or is not text at all.
If you are certain that the property list is a text property list, you can use:
NSStringEncoding enc;
NSString *contents = [NSString stringWithContentsOfFile:defaultsPath usedEncoding:&enc error:&error];
This will allow the framework to determine the encoding of the text plist for you. If it isn't a text plist, you can convert it to one using the plutil command line utility:
plutil -convert xml1 file.plist
Or, alternatively you can do this in code by loading the plist using the NSPropertyListSerialization class, obtaining the NSData from it that represents the plist as the XML format, and then convert that to a string.
An example would be [uncompiled and untested]:
// load the file as-is into a data object
NSData *data = [NSData dataWithContentsOfFile:defaultsPath];
// convert the plist data into actual property list structure
id plistFile = [NSPropertyListSerialization propertyListWithData:data
options:0
format:NULL
error:&error];
// get the XML representation of the property list
NSData *asXML = [NSPropertyListSerialization dataWithPropertyList:plistFile
format:NSPropertyListXMLFormat_v1_0
options:0
error:&error];
// convert the NSData object into an NSString object
NSString *asString = [[NSString alloc] initWithData:asXML encoding:NSUTF8StringEncoding];
This should work whether the original plist is in XML or binary format. In this example, I am assuming that the XML representation of the property list is in fact UTF-8 encoded, as this is the most common encoding for XML data.

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]];

NSSring from plist created from NSArray

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.

Resources