Xcode IPhone Parse Server Output - iphone

I am sending some data to the server that is coming back as a string, but i need to parse it at the carriage return (new line)
The variable the output is being stored in is
NSString *serverOutput = [[NSString alloc] initWithData: dataurl encoding:NSASCIIStringEncoding];
The output is:
1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2
Any suggestions or help?

Something like this:
NSString *serverOutput = #"1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2";
NSArray *myData = [serverOutput componentsSeparatedByString:#"\n"];

Related

iPhone: strange output when casting nsdata to nsstring

I get a NSData object in return after decrypting a payload with aes128:
NSData *returnData = [ciphertext AES128DecryptWithKey:keyData withIV:ivData];
I get the following hex output when i try to NSLog this:
<2db88b73 d84599a1 5779c736 09c975b7 92750cf2 d11cb41b 19f13781
4401bc57 b2ad96c8 402e3ccf 851c0219 00aec76b>
I then try to setting it as NSString:
[[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
When using NSLog() on the string i get "(null)" as output.
Can someone tell me why and where i should look for the problem?
Collided with the same issue some time ago, found the answer here.
If the data is not null-terminated, you should use -initWithData:encoding:
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
(If you have ARC enabled, remove the -autorelease call.)

convert emojis into unicode dynamically

How should i convert the emojis into unicode and send it to server and then can decode it dynamically.
I can statically give the if condition for perticular emojis.But i want to make it dynamically.
Thanks in advance
few days ago i have same problem. i tried this its work for me :
to pass value to the server
NSData *data = [textView.text dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
goodValue = [goodValue stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
when fetch value from the server
NSString *goodValue = [StaticClass urlDecode:[dataDict objectForKey:#"comment_text"]];
NSData *newdata=[goodValue dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *mystring=[[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];

convert NSString to UTF-16 compatible format

I am working on emojis with iPhone. I want to convert something like "\u1F604" (NSString) to "\U0001F604"(NSString) format.
How can i do that?
Thanks,
Jim.
Please try the below code.
NSString* resultTemp = #"\u1F604";
NSData* data = [resultTemp dataUsingEncoding:NSUTF16StringEncoding];
NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
self.detailDescriptionLabel.text = result;

Can not read file in XCODE 4.2, worked in 4.0?

I have upgraded from XCODE 4 to 4.2 and now i have problems.
The following code worked pre 4.2 to read the file in "filePath":
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
// Load array
NSArray* myArray = [myString componentsSeparatedByString:#"\r"];
NSLog (#"\n \n Number of elements in myArray = %i", [myArray count]);
With 4.2 the "initWithContentsOfFile" in the following code line is deprecated:
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
...and should be replaced with the below according to the manual:
NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];
and i can not get this to read the records in the same file by replacing the code line. BTW, i have defined the &err.
When i NSLog myString i get (null).
I am getting a bit desperate to solve this and would very much appreciate any help.
Cheers
NSLog the err variable if there is an error. Also NSLog filePath.
Perhaps the encoding is not UTF-8, are you sure about the encoding?
The best non-UTF-8 encoding bet is NSMacOSRomanStringEncoding which supports 8-bit characters.
Try :
NSError* error = nil;
NSStringEncoding encoding;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
If that does not works, try in your code : NSASCIIStringEncoding
The file probably doesn't contain a UTF8 encoded string. See apple's documentation, which has an example of reading a file where you do not know the encoding: Reading data with an unknown encoding
You need to use the [string|init]WithContentsOfFile:usedEncoding:error method, and if that fails there are a few more things you can try before finally presenting an error message to the user (for example, try reading it as an NSAttributedString).
For example, you could do this:
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSStringEncoding encoding;
NSError *error;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
if (!myString) {
myString = [[NSString alloc] encoding:NSUTF8StringEncoding error:&error]
}
if (!myString) {
myString = [[NSString alloc] encoding:NSISOLatin1StringEncoding error:&error]
}
if (!myString) {
NSLog(#"error: %#", error);
return;
}
// Load array
NSArray* myArray = [myString componentsSeparatedByString:#"\r"];
NSLog (#"\n \n Number of elements in myArray = %i", [myArray count]);

iPhone - Convert NSString encoding from WindowsCP1251 to UTF8

how can I have this conversion from NSWindowsCP1251StringEncoding to UTF-8?
I had several attempts but no one worked as it should. My last try was:
NSData *dt = [mystr dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSWindowsCP1251StringEncoding];
The result of str is unreadable. Did anyone encounter anything similar?
I think you were so close:
// Convert it back to CP1251
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];
// Now load it as UTF8
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];