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

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]);

Related

Parsing XML using GDataXML returns a NSString object with a retainCount of 95.How to ensure it gets released?

The NSString object that is got by the following code has got a retainCount of 95.
for(GDataXMLElement *ele in [doc.rootElement elementsForName:#"myKey"])
{
NSLog(#"myKey %d",[[[ele.children objectAtIndex:0] stringValue] retainCount]);
[myDict setObject:[[ele.children objectAtIndex:0] stringValue] forKey:#"myKey"];
}
. so would it get released later when
[myDict removeAllObjects];
[myDict release];
is called.
the problem i am facing is that i have hundreds of strings like this parsed.... and all their retaincounts are around 95...would these strings get released?
the problem i am facing is that i have hundreds of strings like this
parsed.... and all their retaincounts are around 95...would these
strings get released?
First, retainCount is useless. Don't call it. No, really, don't use retainCount.
To answer your question, look to Instruments. Do the objects that you expect to go away actually stay in memory? If so, then turn on reference count tracking and see what still holds references to them (or what retains are unbalanced).
More likely than not, the XML subsystem is unique-ifying the strings such that only one copy of what may be repeated hundreds of times is in memory. That one copy may be retained dozens or hundreds of times as a result. When you removeAllObjects from myDict, there may still be a reference to the objects. It might even be an autoreleased reference and, thus, will actually go away.
The only way to know is to look to see if the objects are deallocated via Instruments (or some other means).
As per the definitions & explanations given by many others including raywinderlich in his How To Choose The Best XML Parser for Your iPhone Project.
GDataXML is nothing but a NSXML style DOM XML parser for the iPhone, developed by Google as part of their Objective-C client library. Consisting of just a M file and a header, it supports both reading and writing XML documents and XPath queries.
A DOM parser reads the entire document and builds up an in-memory representation that you can query for different elements. Often, you can even construct XPath queries to pull out particular pieces.
So it just creates a tree like structure for the given entire XML, each elements can be queried to pull particular pieces.
And as we know this all works with the pointers, so what ever elements we get from this tree will be just pointing to that object, with the same name(used while initializing xml) or a new name while pulling particular pieces(using NSXMLElement).
And so everything will be retained until we release the XMLDoc while initializing XML.
If you want we can check the retain counts after releasing the XMLDoc(but it may give crash as are we using released object).
I think it works in this way, if you or other developers have any other info on this share your info.

reading file from Lua in cocos2d-x

I am working on a cocos2d-x project in Lua. I have a problem with syntax openien file, when I invoke:
cocos2d.CCFileData:new("file.txt", "w"+)
I always get error:
Cocos2d: ...80-C048-4F64-BC52-4849C0AD02F5/Main.lua:30: attempt to index field 'CCFileData' (a nil value)
What am I doing wrong? i cannot get any doc info. I am doing this on iOS.
1) Are you sure the call must be prefixed with "cocos2d"? Without knowing cocos2d-x but according to the error message maybe this is the correct call?
CCFileData:new("file.txt", "w+")
2) The "w"+ is certainly incorrect. It needs to be "w+" all in quotes:
cocos2d.CCFileData:new("file.txt", "w+")
Well the CCFileData is just not implemented yet in Cocos2d-x. I have managed this by using io.open lua funcion. It helped to have Lua file routines.
Use this type of call:
xmlFilePath = CCFileUtils::fullPathFromRelativePath("NameOfFile.xml");
I just spent a decent 30 minutes looking for a working version of this. The Cocos2d author, in January, recommends using a different file which I have yet to get working, but I just tested this and I am successfully reading an XML file finally. Thought I'd share this method, since iOS usually uses
[[NSBundle mainBundle] pathForResource:#"" ofType:#""];
styles to get the paths. We have to do similar, and that's the cocos2d-x method of doing it.

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.

Why my app crashes when copying an array with certain strings to a dictionary?

I'm working with a tableview that shows an array of dictionaries. Everything was working fine until I wanted to add an array of strings to the new dictionaries.
The strings in the array have the format "CALL 34". When I add the array the app shows the tableview again but after a second it crashes without showing anything at the console.
The very rare thing is that the app works fine if the strings are between "CALL 1" and "CALL 32", or between "CALL 99" and "CALL 100". The console shows the "modifying layer that is being finalized" error when it works tho.
I NSLog'd the array before adding it and the strings are all fine.
Any idea would help as I'm completly lost!
Thanks
I tryed not releasing anything in that class and the problem is solved (I should find what was making it crash...). The class is a heritated class.
Without showing code, it is impossible to say exactly. As well, if there is a crash, there is a backtrace. Show it, too.
Sounds like a memory management issue.