How to get a number from a text field? - iphone

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.

Related

how to provide name-space to use xpath query in objective-c

i am trying to use xpath-query in my app for xml parsing. my work is almost completed upto name space concept came into picture.i am always writing query correctly but its returning me the 0 objects in my array.i tried many blogs and also many posts in our stackoverflow.com but none of them not working..i hope my problem need small modification can any one modify my code.my code
Thanks All.
I think many people try to show my question is duplicate but it is different bec none of the questions not worked for my problem.please provide me your suggestion.your answer more help full for my work.
I take it you're using libxml along with Matt Gallagher's Cocoa wrappers for it? (It's helpful to put which third party libraries you're using in your question.)
When I did this, I modified Matt's code so that PerformXPathQuery took an additional argument, an NSDictionary of namespaces. (The other functions such as PerformXMLXPathQuery also took this argument, and pass through the object.)
Then, in PerformXPathQuery, after creating the context, I registered the namespaces thus:
if (namespaces != nil)
{
for (NSString *key in namespaces)
{
xmlXPathRegisterNs(xpathCtx, (const xmlChar *)[key UTF8String], (const xmlChar *)[[namespaces objectForKey:key] UTF8String]);
}
}

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.

Grabbing text from files

Still working my way through this program. Next task on my to-do list is selecting random words from a pre-generated list. I've got the randomisation code sorted, but I now need to know the best way to store and retrieve words from my big list (and it is a fairly big list - over 220 words).
Since I'm designing for iPhone, memory is a paramount concern. Because of this I was hoping to avoid loading the whole file into memory. I'd much rather have the file laid out so that I can jump straight to an indexed position in the file and grab only the data I need. It would be nice if I could make use of the text file I already have with all the words in it, but I don't mind converting if there is benefit to some other method.
Anyone got any suggestions about the best way to do this?
-Ash
Well, 220 words isn't exactly a big list :-) Let's say each word is long, say 20 characters. Then you're talking about a measly 4.4kB. So I wouldn't worry about the size here. As Kevin pointed out, [NSArray arrayWithContentsOfFile:...] is likely the easiest way (also have a look at [NSDictionary dictionaryWithContentsOfFile:...]).
But if your list is getting really big (say 10000 words) then I'd suggest you read up on SQLite which is also supported on the iPhone.
Don't worry about the storage space (the storage required is far less than you think). Use a PLIST (File > New File > Resource (Mac OS X) > Property List), and arrayWithContentsOfFile to make loading the words simple (define an array as the root item in the PLIST; Apple's documentation has further details). Then, simply:
srandom(time(NULL));
NSUInteger index = rand() % [array length];
NSString *word = [array objectAtIndex:index];

iphone compare   with NSString

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.

Alternative for print_r in php for iphone(objective-c)?

I've started with objective-c/iphone programming a couple of days ago, and still having some problems converting from other languages.. I'm currently testing around with NSArrays, and NSMutableArrays, and it's really annoying that I haven't found any method for effectively displaying the contents of my arrays (without creating loops and displaying row by row).
Is there some easy method like print_r in PHP or console.log in javascript(w/Firefox && firebug) ?
Really appreciate any help :)
Use NSLog, e.g.:
NSLog(#"This is my NSArray: %#", [myNSArray description]);