iOS special characters, strange issue - ios

I've got a very strange issue. I started an iOS App about three years ago (iOS-SDK 3.0), and since then went through the SDKs 4.0 and 5.0. Since 5.0 (or maybe 5.1) I suddenly started having problems with German special chars (ä ö ü ß).
Now I can't even initialize an NSString with special chars, this line:
NSString *str = #"abcäxyz";
gives the following warning:
Input conversion stopped due to an input byte that does not belong to the input codeset UTF-8
And this one:
NSLog(#"%#", strTemp);
gives:
abc
So it's stopping at the first special char. In other projects everything is fine. I can work with special chars without any problems.
Is it a configuration problem?
EDIT: Obviously it is a problem with the file encoding.
file -I myFile
is giving:
text/x-c++; charset=unknown-8bit
Trying to convert it with iconv gives me:
conversion from unknown-8bit unsupported

What happens when you use the UTF-8 codes to initialize the string? Like so:
NSString *s = [NSString stringWithFormat:#"%C", 0xc39f]; // should be ß
As far as I know you should also be able to do this, but haven't tested it:
NSString *s = [NSString stringWithUTF8String:"0xc39f"];
Try those and see what happens. There's a number of sites around that keep UTF-8 code tables for special characters, e.g. this one.

As long as your file is encoded UTF-8, #"abcäxyz" should be fine, but the explicit form of embedding a literal unicode characters is \u????.
- (void)testGermanChar
{
NSString *expected = #"abc\u00E4xyz";
NSString *actual = #"abcäxyz";
STAssertEqualObjects(expected, actual, #"the two strings should be equivalent");
}

SOLVED: Changed the file encoding in Xcode:
Click on the file you want to change the encoding of, then open the right panel (whats the name of this pane actually? any idea?) to edit the properties. There you see "Text Encoding" under "Text Settings". That is all.

Related

Unity / Objective-C: Trouble converting char* to NSString

I have what should be a dead-simple piece of code that is failing for me in strange ways.
void MediaShare(char* text, char* furl) {
NSString *status = [NSString stringWithUTF8String: text];
NSString *media = [NSString stringWithUTF8String: furl];
[[SocialShare sharedInstance] mediaShare:status media:media];
text is just a line of text for Twitter sharing, and furl is just a file location string. I am getting a crash down past this function that comes down to bad data getting passed. Putting a breakpoint at the head of this function yields the following-
Image of Xcode variable monitor
The two values look fine, although not sure if the * values that only contain the first char are a problem.
Anyway, jumping to the end, and status and media appear to be converted to hex values.
Converted to hex values?
Any ideas? To give the full story, this is a Unity plug-in. I am simply passing in two strings from Unity C# to this function.
Thanks so much.
The code looks fine so far, if the input values are well formed C char* strings that can be interpreted as an UTF-8 encoded string.
status and media being hex values at the end of the function hint that they are. Both are pointers to Objective-C objects, so this is expected. Print them to the debug console or use po status at the debug console to check their contents (it will print the result of the -description method of status which is the string content in that case).
The subsequent crash might be caused elsewhere in the code.
What's the crash's log output?

Can't decode NSString well

I had my server encoding in ISO-8859-1 and decided to chango into UTF-8. The problem is that the filenames creates in ISO now crashes because I change the encoding type in my code. I try to connect with an iOS app and show the directories and files, the news are shown well but the olds with ISO not.
How can I detect if the filename is in one or other encoding to process in the right way each one? Because now, the filename in ISO it can be represent in UTF-8 but the string to access is not the same. (Ex: %E1p%F1 --> %C3%A1p%C3%B1)
I try this, but it doesn't work:
NSString *isoL = [item.href stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
char const *iso2utf = [isoL UTF8String];
NSString *utf = [[NSString alloc]initWithCString:iso2utf encoding:NSUTF8StringEncoding];
You need to have the server declare the content encoding of the response, much like how HTTP does.
I don't believe it's possible to auto-detect ISO-8859-1/UTF-8 text encoding.

iOS Unicode Encoding

I am getting this kind of data ElbowWristHand_DeQuervian\U00e2\U0080\U0099s Tenosynovitis through a web service, But actually the content is "ElbowWristHand_DeQuervian's". I have followed all the methods mentioned in the following link:
Using Objective C/Cocoa to unescape unicode characters, ie \u1234
but still i am unable to convert the unicode characters to the proper string. Please suggest.
Thanks
While initializing string us unicode string encoding -
NSString *dataString = [[NSString alloc] initWithData:recievedData usingEncoding:NSUnicodeStringEncoding]

how to remove characters '\U0000fffc' from nsstring ios

I am developing chat application. In iOS 8, i am sending text and image, while sending image with text a characters "\U0000fffc" are prefixed to nsstring.
I tried using the below code,but it is not working
NSCharacterSet *characterset=[NSCharacterSet characterSetWithCharactersInString:#"\\U0000fffc"];
NSString *newString = [str stringByTrimmingCharactersInSet:characterset];
Any help would be appreciated
Thanks to All who have answered and contributed. I fixed using below code
NSString *codeString = #"\uFFFC";
NSString *msg=[msg stringByReplacingOccurrencesOfString:codeString withString:#""];
If I used above code then compiled errors are fixed and worked....!
NSString *str = #"This is my string \U0000fffc";
NSString *strModified = [str stringByReplacingOccurrencesOfString:#"\U0000fffc" withString:#""];
NSLog(#"%#",strModified);
Try this. Hope will work for you. :)
Today, I have faced the same situation and after digging in some more I have found that the said character is in fact NSAttachmentCharacter which the function -(void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; adds to your attributed string to mark the position of a NSTextAttachment in your string and you shouldn't replace it anyways.
If you are still facing trouble working with your Strings, provide more information about the problem, otherwise, stick to your current implementation.
This is the replacement character � (often a black diamond with a white question mark or an empty square box) is a symbol found in the Unicode standard at codepoint U+FFFD in the Specials table. It is used to indicate problems when a system is unable to render a stream of data to a correct symbol. It is usually seen when the data is invalid and does not match any character.
This can be removed as follow
NSString *codeString = #"\\ufffc";
NSString *newString=[str stringByReplacingOccurrencesOfString:codeString withString:#""];
P.S : "/uFFFC" didn't worked for me.

my apostrophe turns into ’

i'm trying to send the iOS device name along with the deviceToken to my server in didRegisterForRemoteNotificationsWithDeviceToken.. my device has an apostrophe in it and the request string shows as ....&name=John+Doe’s+iPhone ...
I've tried replacing "'" with "" and "’" with "" .. but neither fix it.. i assume i need to convert the encoding?
NSString *string = [[UIDevice currentDevice] name];
output-> John Doe’s iPhone
EDIT
output is from
NSLog(string);
You should never use NSLog with a user-provided string like that. NSLog expects a format string for its first argument. If it's something containing a format specifier, it will probably crash your application, and may expose a security hole if provided by an untrusted source.
Your encoding issue appears to be a bug in LLDB. Switching to GDB fixes the problem.
Wait, you're doing NSLog(string)? You need NSLog(#"%#", string)

Resources