iphone compare   with NSString - iphone

i was comparing A string coming from XML with another String and results were showing that they are not equal. but in NSLog() both were same ( e.g. Valore Books ).
then i checked the Source of the XML and i came to know that the actual string is "Valore Books" and   is infact a space. but the problem is this when i am comparing it with #"Valore Books", it is saying both are not same.
What to Do ??

Note: I'm replacing my original answer with one that's actually correct for this problem. Sorry for the initial misunderstanding.
The following line will unescape the html entities in your string.
NSString *A = #"Valore Books";
NSString *B = (NSString *)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)A, NULL);
I couldn't find any equivalent function that was higher level, but the performance of this should be excellent. If I read the docs correctly, you can pass in a CFDictionaryRef as the third argument to specify extra conversions, but it seems that this does a good job doing standard ones on it's own.
Docs are here.
Note that it's probably a good idea to handle the encoding whereever you're pulling those strings into your program at, and not everytime you're comparing.
Also found a second part of this you need to consider. &#160 isn't just a space, it's a non breaking space, which the above code converts to \312 instead of the standard space. Those are in fact separate characters in the encoding and when you do a string compare it will fail.
Maybe it'd be easiest to replace #160 with #32 using
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
and then running it through the unescape.
It also just occurred to me that the CFXMLCreateStringByUnescapingEntities won't be available on the iphone. Here is a link to an example that shows how to do similar conversions on the iphone.

  is a non-breaking space (Unicode value U+00A0)
The regular space is (in #"Valore Books") has Unicode value U+0020.
So it is not the same character, and the two strings are not equal.

i solved the problem using a string parsing utility provided by google. Thanks everybody.

Related

Identifying and formatting XML String to readable format in XMLParser

I am working in Swift and I have a set of Data that I can encode as a String like this:
<CONTAINER><Creator type="NSNull"/><Category type="NSNull"/><UMID type="NSArray"><CHILD>d1980b265cbd415c90f5d5f04efcb5df</CHILD><CHILD>7e0252c137c249fc92bd0f844effe27f</CHILD></UMID><Channels type="NSNumber">1</Channels></CONTAINER>
I am looking for a way to format this string as XML with indents so I can use XMLParser to properly read through it, which it currently does not. I imagine NSNull is when the object is empty, I just haven't seen this format so I don't know what to search for. Could it be closer to a Dictionary object? If so I'd be happy to format it as that as well.
I've also tried to create a XMLDocument from the data, but it doesn't fix the format.
EDIT:
I wanted to add a bit more information to help clarify what I am trying to do. This string above is derived from an encrypted piece of metadata from a file. In my code I identify the chunk of data that is encrypted, then decrypt it, and then convert that data to a string. It's worth noting that the string ends up having null characters in between each valid character, but I strip those out and end up with this string.
Copying this string into an XML Validator confirms it is valid XML. What is confusing to me is it's format, in which it has Object types such as NSNull and NSNumber. My post was originally hoping to identify this type of format. It seems like more than just XML.
In response to some of the comments, I have used XML Parser delegate with other XML strings and have a basic understanding of how it works. I should have originally mentioned that and instead said that XML Parser does not recognize any of these elements or strings within them.
UPDATE:
The issue ended up being the null characters in between each valid character. Stripping those out and then running it through XML Parser worked great. Thanks all.

Sending a JSON NSData via URL

I am attempting to send a JSON representation of an object in an email link. The recipient will open the link and my app will respond via a url scheme. It must extract the JSON from the url and re-build the object.
I am serializing my object by building an NSDictionary and using:
return [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
I'm not sure what comes next. Somehow I need to convert this NSData into a string so that I can prefix my url scheme and use it in a link.
On the receiving end, I then need to remove the prefix (which I can do) and turn the string back into an NSData.
What is the correct method for doing this? And how do I make sure that the contents of my data do not interfere with the JSON string encoding (e.g. if my object contains text including special characters)?
You need to do an additional encoding step, since there are characters in encoded JSON that also have significance when they are part of a URL. What we actually want to do is URL-encode the data so none of the characters in the resulting string conflict with what applications expect a URL to look like.
The first step is transforming our data into an NSString (this is basically just a memcpy since NSStrings are encoded in UTF-8 by default):
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now, there's a function you might be tempted to use called -stringByAddingPercentEscapesUsingEncoding, but it doesn't do a thorough enough job of escaping all the relevant characters, so we need to build our own.
I could repeat the code here, but since it's been done many times already, just view this blog that shows how you can add a category to NSString to do proper encoding, after which you can append it and send it on its way. Writing the analogous decoding function with CFURLCreateStringByReplacingPercentEscapesUsingEncoding is an exercise for the reader of which many examples can be found floating around.
Make sure your payloads are quite small (on the order of a couple of kB), by the way, since there is probably an upper bound on how long URLs, even those used locally and with a custom scheme, can be.

How to get a number from a text field?

How I can scan a number from a text field?
For example, in C
int x,area;
scanf("%d",&x);
area=r*r*3.14;
printf(" the area is %d",area);
How to do this on iPhone in Objective-C with a text field?
Check out NSScanner. It performs a similar function to scanf, but works on NSString objects.
if it's in a textfield, you can do
double r=[textFieldName.text doubleValue];
NSString *result = [NSString stringWithFormat:#"the area is %f",r*r*3.14];
someLabelYouAreUsingToDisplayResults.text=result;
What you wrote works perfectly fine in Objective-C (or rather would, if it wasn't buggy). Everything that works in C works in Objective-C, without modification.
That said, there are more idiomatic ways to do such things in the Objective-C language (and Cocoa libraries), which I'm sure others will point out to you. See Robot Woods's answer for an idiomatic example of how to read from a text field, for instance.

Parsing HTML with XPath in Objective-C

Hey guys, I'm trying to parse HTML with XPath from http://lib.harvard.edu/libraries/hours.html in Objective-C for an application that shows the operating hours for each day of the week at each of the 50 libraries listed on the website. I found code to facilitate XPath parsing of HTML in Objective-C at cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html, but I'm still a little confused about how I should go about obtaining the hours for each day for each library. The relevant method to use seems to be
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query)
and my code so far is
NSURL *urlPath = [NSURL URLWithString:#"http://lib.harvard.edu/libraries/hours.html"];
NSArray *array = PerformHTMLXPathQuery([NSData dataWithContentsOfURL:urlPath], NSString *query);
but, since I've never used XPath before, I'm not sure what string I should use in the second parameter of the method. Does anyone have any ideas?
Also, I'm not quite sure what to do with the array that gets returned by PerformHTMLXPathQuery(). I feel like cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html gives a pretty good explanation, it's just that I've never used XPath before so it doesn't make much sense to me at this point. So, to summarize, as long as my code so far is correct, I want to know what to use for the second parameter in the PerformHTMLXPathQuery() method and how to extract the relevant data from the array it returns. Any help would be much appreciated!
XPath is a language for navigating XML documents. The query parameter is an XPath query string, which you hope will be able to extract the elements you want from the HTML file. I say "hope" because
I don't know how well XPath plays with HTML 4 documents
I've had a look at the source of the page you want to parse and it is quite complex.
Anyway, those points aside, you'll be wanting to learn how to create an XPath expression. Fortunately, Google is your friend and typing "XPath" into it brings up the W3Schools tutorial on XPath. I have only skimmed it but it looks like what you need.

What's the point of -setFormatWidth: or -formatWidth from NSNumberFormatter?

The documentation is hard to interpret on this topic. Does this value return me how many characters the calculated format string will contain? Or does it tell me how big actually that string is? Or can I tell it how big (in unit squares or "pixels") the output should be?
Maybe someone can point out what this methods do...
The -formatWidth getter method tells you how many characters the string will contain. The -setFormatWidth allows you to set that width.
The 'drawInRect' NSString extension can be used to constrain a string to a specific rectangle size.
Those same extensions have a method called 'sizeWithFont' that will tell you how big the rectangle needs to be to hold the string given a certain font.
See: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html