I'm using NSFileManager to create a file at a specified path with text view contents.
I'm using following code
[[NSFileManager defaultManager] createFileAtPath:saveFileName contents:[[tView text] dataUsingEncoding:NSASCIIStringEncoding] attributes:nil];
But the problem is that, its creating file with no contents. I'm sure that text contents are not nil.
How can I check and confirm that data is written into file.
Some suggestions:
Capture the BOOL return of the line to make sure it is completing.
Log the value of [tView text] to make sure it is not empty.
ASCII encoding can cause problems these days. Try NSUTF8StringEncoding or NSUTF16StringEncoding.
Localization may be a problem with plain ASCII encoding because ASCII does not handle multibyte character encodings.
Well, I know it's few months late, but you're sending NSString instead of NSData to the contents: parameter.
Try:
[[tView text] dataUsingEncoding:NSUTF8StringEncoding]
Related
EDIT: NSLog output works well in the simulator, but doesn't work when connected to a real device. And it seems that it is a bug — http://openradar.appspot.com/11148883. Also it happens that it is related to the LLDB, switching Xcode to GDB resolves the problem. Either it's possible to JetBrain's AppCode, which works well with the LLDB.
I have a bunch of unicode strings in the application, and if I try to output any of those strings using something like NSLog(#"%#", aString) then all the ASCII characters in the string will be printed fine but all the cyrillic letters will be messed up, so instead of
newLocation: coordinate:60.019584,30.284954 'Удельная'
I'm getting:
newLocation: coordinate:60.019584,30.284954 '–ü–æ–∫–ª–æ–Ω–Ω–æ–≥–æ—Ä—Å–∫–∞—è'
And that's quite hard to do any debugging with that kind of output. And because that app is targeted for the Russian market only I can't just change locale and use English strings.
So I wonder if there any way to make NSLog work well with unicode characters? And I'm looking only for some kind of one-liner solution, I know that there are some ways to write half a page of code and output unicode chars, but I'm looking for something shorter. Ideally I'm looking for some method of NSString that will make it all work. e.g.
NSLog(#"%#", [aString someThingThatMakesUnicodeWorkWithXcodeConsole]);
Yes, obviously you can create a string that will contain and output cyrillic letters. When I was learning Objective-C, I had the same problem in the begining(I'm as well was working with Russian words and stuff like that). So solution is to convert the string to other format like this:
NSString *string = [NSString stringWithCString:"Привет, как дела?" encoding:4];
NSLog(#"%#", string);
or
NSString *string = [NSString stringWithUTF8String:"Этот вариант короче!"];
NSLog(#"%#", string);
Hope it helps you!
P.S It means that you need to make create your strings as C-Style Strings, and set their encoding parameter to 4(UTF-8). You can see all list of avaliable parameters in the documentation to NSStringEncoding in NSString.
As far as I know it is relevant to NSLog() and LLDB on some Xcode versions. Have a try with one of these solutions:
Check log in Xcode Organizer >> Devices >> your device >> Console.
Use GDB as your debugger instead of LLDB if you are using the latter one. This can be changed from the schema options. Please refer to the steps in the comment by "cocos2d man" below.
Upgrade to Xcode 4.3.2. Some people say it solved this issue, but I haven't confirmed this myself.
Try to convert it in to UTF8 string.
NSString *str = [aString UTF8String]
NSLog(#"%#", str);
Hope this helps.
Try putting it like NSLog(#"%#", aString);
EDIT :
you can convert it in UTF8 string. This could get you through.
NSString *str = [aString UTF8String];
Hope this helps.
Try this. It works for me.
NSLog(#"%#", [NSString stringWithCString:[[places description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
I want to encode a short title in filenames. The problem is that occasionally the title will contain a character such as a colon or a slash. Is there a standard encoding that would be typical/appropriate for this?
EDIT: to clarify, I want to encode the title in such a way that the encoded title could be used as a filename. Or is that called percent escaping?
The way I do this is with a category on NSURL, which I use to get the NSURL for a filename in a particular directory. Once I have this NSURL, I can fetch or save the file using the URL after performing the usual checks about whether or not the file already exists and handling those cases accordingly.
The relevant code snippet is:
+ (NSURL *)adnURLForFileName:(NSString *)fileName inDirectory:(NSSearchPathDirectory)searchDirectory {
NSString *percentEscapedFileName = [fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *URLForDirectory = [[fileManager URLsForDirectory:searchDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
return [NSURL URLWithString:percentEscapedFileName relativeToURL:URLForDirectory];
}
You can download the full category code from GitHub - NSURL+ADNFileHelpers
You could use -stringByReplacingOccurrencesOfString:withString: to replace the slash character with U+2044, the "solidus" aka "fraction slash". It looks like this: ⁄
http://en.wikipedia.org/wiki/Solidus_(punctuation)
The slash is not allowed in the Unix APIs. The colon is not allowed in HFS and in the old File Manager APIs. The same filename character will show up as a colon in the former and as a slash in the latter. In practice: you can use the Finder to rename a file to "/" (because the Finder uses the traditional Mac separator of :), but it will show up as ":" if you use ls.
If you need to allow both colons and slashes, you need to encode the characters somehow. You could use URL-style escaping, but if you expect the user to look at the filename in the Finder or in some other program, it's going to look horrible. It's better to escape just the path separator. For example, if you're using the Unix style APIs (path separator /), you could encode / as :- and : as :: (to avoid ambiguity). Or you could use some other little-used character for the escape.
I have approached this problem by filtering the title before using it in the filename. NSString has some useful methods, such as stringByStandardizingPath and stringByReplacingOccurrencesOfString:withString:. The filtering approach is lossy, in that the original title information might not be restorable. Similarly, I don't think encoding would work because iOS allows such a wide range of characters in its filenames. One possible alternative solution could be a plist archive with key=filename, value=title.
i'm saving a NSString inside an NSArray and that NSArray inside an NSDictionary. While doing this, a process inside my NSDictionary notifies me if my string is like Hi I'm XYZ. Then in the place of single quote the appropriate UTF character is getting stored.
So how to avoid this or how can I get my actual text along with special characters from NSArray or from my NSDictionary?
Any help is thankful.
NSString internally uses Unicode characters. So it easily can handle all sorts of characters from different languages.
You cannot choose the internal encodig of NSString. It's always Unicode. If you have an encoding problem, then you have either created the NSString instance incorrectly or you have output the instance the wrong way.
And there's no such thing as an UTF character.
Please better describe your problem and show the relevant source code.
I am making a simple CSV file in my application, and I use characters such as 'æøå'. When I open the file on my Mac with BBEdit it opens the file correctly, but if I import the file in Excel (MS Office 2011) with Unicode UTF-8 it does not show the characters correctly. How do I fix this?
Some of the code:
NSString *tmpPath = NSTemporaryDirectory();
NSString *fullPath = [tmpPath stringByAppendingPathComponent:#"log_export.csv"];
[csvComplete writeToFile:fullPath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
I am mailing the csv.
[mailController addAttachmentData:data mimeType:#"text/csv" fileName:#"sertifikat-loggbok.csv"];
CSV is a text format that does not specify the character encoding. Different applications assume different encodings.
My experience is that Microsoft Excel assumes it's in the default encoding which happens to be CP1250 (similar to ISO 8859-1 / Latin 1 encoding) on my machine. Maybe it always assumes CP1250.
So try to switch from UTF-8 to CP1250. But I cannot guarantee it'll always work because there simply is no encoding specified for CSV.
On some of my strings there seems to be somekind of newline char. I think this is the case because when i do a simple NSLog
NSLog(#"Test: %#",aNSMutableString);
I would get output like below
Test:
I am a String
I've tried using
[mutableString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
But it does not remove whatever it is thats forcing the newline to happen.
In a string that i parse out from a file which has 4 characters 'm3u8' has 5 chars when I check the length of the new string.
Anybody got an idea of what might be going on?
Thanks
-Code
P.S.
I know I could just zap the first char out of all my strings but it feels like a hack and i still wont know whats going on.
[mutableString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
The above will not directly modify your mutableString. It returns a new autoreleased NSString with the characters trimmed. See NSString doc.
e.x.
NSString *trimmedString = [mutableString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSLog(#"Test: %#", trimmedString);
should give you expected results.
I think #Sam 's answer will fix your problem, but I think the origin of your problem is the file source. Do you know how it is encoded? Is it part of a download? My guess is that you have a Windows' file with "\n\r" terminating lines and you are using Unix string tools that are breaking on "\n", thus leaving a leading "\r".
Verify the source of the file and read the document lines with the appropriate encoding.