How to display complex object in debugger? - iphone

I'd like to display the contents of the property myarray, from the following singleton:
[Session sharedManager].myarray
I've tried these:
po [Session sharedManager]. myarray
po [[Session sharedManager] myarray]
but always get this error:
A syntax error near end of expression.
Any suggestions?
--- EDIT ---
I'm working with SDK 3.0.
I've discovered the problem, which is I had three open brackets rather than two. You can't see that here because I mistyped the number of brackets. It is working now. Thanks.

If you use XCode Debugger and you set a breakpoint in the some place that the variable is already initialized and can be seen, you can click to the variable and choosing Print Description.
You can do it in simple way like NSLog(). What is the problem with this approach? Usually, I see that it will print out all the description() method of all objects in the array?
And I am not sure, but you lack a semicolon at the end of the statement. ";", can you recheck for that one?

What you are describing is very strange. I set up a test application and was able to print the object from the singleton just fine.
#import "testAppDelegate.h"
//A Session Singleton
#interface Session : NSObject {
NSArray *myArray;
}
#property (nonatomic, retain) NSArray *myArray;
#end
#implementation Session
#synthesize myArray;
static Session *sharedSession;
+(Session *)sharedSession {
if (!sharedSession) {
sharedSession = [[Session alloc] init];
sharedSession.myArray = [NSArray arrayWithObjects:#"A",#"B",#"C",nil];
}
return sharedSession;
}
#end
//App Delegate
#implementation testAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"%#",#"Breakpoint Here"); //Here is where I set My breakpoint
return YES;
}
- (void)dealloc {
[super dealloc];
}
#end
In GDB:
(gdb) po [[Session sharedSession] myArray]
<NSCFArray 0x4710630>(
A,
B,
C
)
I did this using the 3.2 iPhone SDK, using a default project template, in the debug mode without changing any build settings. I suspect you may have issues in your build settings. I have noticed that debugging is wonky on the 4.0 beta sdks. If you are using 4.0, remember that it is still beta, and your problems may really be someone else's problems.

Related

property loses its value (long but easy to understand)

In my app, I have:
car.h
#interface car : NSObject
{
NSString *model;
NSString *price;
// others atributes
}
#property(nonatomic, retain) NSString *model;
#property(nonatomic, retain) NSString *price;
...
myshop.h
#import "car.h"
#interface myshop : UIViewController...
{
car *mycar;
}
#property(nonatomic, retain) car *mycar;
...
myshop.m
...
-(void) viewDidLoad
{
...
mycar = [[car alloc] init];
}
so, I have a method that shows a popover, where I can select a car from a tableview. This popover callback a method in the myshop.m, using delegate, where I assign a value to mycar.model, and call the method doA above, all of this works fine, and shows the value of mycar.model in Output:
-(void) doA
{
NSLog(#"car = %#", mycar.model );
...
}
But... now it is the problem: I have a buttom in the myshop view. When I press this button, the action shows an alert view (there is the delegate in .h). The return of this alert calls:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self doA];
...
}
And the program crash when doA tries to write mycar.model to Output. No error is showing in Output View of Xcode. In the code, it shows: Thread 1: Program received signal: EXEC_BAD_ACCESS.
I can show as many cars I want from popover view, but when press the button, and the AlertView closes, the program crashes.
Just for test, I call [self doA] in other methods that runs from another class via delegate and always the app craches.
Any idea, what is wrong?
Completing the code
Is there any error here? (this is a method of car.m)
- (void) setValues: (NSDictionary *) data
{
model = [data objectForKey:#"model"];
price = [data objectForKey:#"price"];
...
I don't initialize the properties (model, price, ...) anywhere in code.
SOLVED !!!
It was a memory management problem!
I change
model = [data objectForKey:#"model"];
by
model = [[NSString alloc] initWithFormat:#"%#", [data objectForKey:#"model"]];
I hope that I am right now! At least the app seens to work well!
Thanks a lot friends !!!
You have got a memory management bug somewhere in your code. I don't know where the bug is, it almost certainly isn't in the code you posted (it all looks perfect to me).
Unless you post more code, we can't help you fix this one.
I strongly recommend you enable ARC. It's fairly new, but nowadays it's old enough everyone should start using it.
There's a very good chance your bug will simply go away if you turn ARC on.
The code you posted looks fine, but somewhere else you must be over-releasing mycar. Profile it with the "zombies" instrument, and it will tell you where it is being released.

How to add additions in a cocoa touch static library

I'm writing a simple static library for myself recently. It include some ui control, macro, and additions of cocoa touch class, but there's something wrong with my code, and I don't know how to solve it.
I did these steps:
create a cocoa touch static library project named Orange, just for test.
add a NSObject subclass named MyMath, implement code.
add 2 files for NSArray addition, implement code.
move the project directory to "/".
create a window based application named TestOrange.
drag the Orange.xcodeproj into TestOrange.
set header search paths to "/Orange/Orange"
set Build Phases like the image bellow showed.
when i run the TestOrange, it can print the result of MyMath, but crash immediately.
MyMath can work, but NSArrayAdditions can't work. I think there's something wrong with NSArrayAdditions.
anyone encountered this problem before? please help me.
thanks in advance.
all code have listed here.
MyMath
#interface MyMath : NSObject {
}
- (NSNumber*)AddA:(int)a B:(int)b;
#end
#implementation MyMath
- (NSNumber*)AddA:(int)a B:(int)b {
return [NSNumber numberWithInt:a+b];
}
#end
NSArrayAdditions
#interface NSArray (Additions)
- (NSNumber*)Double:(int)a;
#end
#implementation NSArray (Additions)
- (NSNumber*)Double:(int)a {
return [NSNumber numberWithInt:2*a];
}
#end
use libOrange
#import "TestOrangeAppDelegate.h"
#import "MyMath.h"
#import "NSArrayAdditions.h"
#implementation TestOrangeAppDelegate
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyMath *mm = [[MyMath alloc] init];
NSLog(#"%#", [mm AddA:12 B:23]);
[mm release];
NSArray *ary = [[NSArray alloc] init];
NSLog(#"%#", [ary Double:13]);
[ary release];
[self.window makeKeyAndVisible];
return YES;
}
#end
Try adding -ObjC and -load_all to your "other linker flags" in your build settings.
Basically, categories on framework classes don't necessarily get linked in unless you specify this flag.
-load_all will force the loading of all compiled classes, which solves the problem.

objective-c/iphone sdk newcomer question

At present I'm building a basic application to learn Objective-C and the iPhone SDK.
I'm creating NSObject with getters and setters to get to grips with how these works. I've successfully added a property and getters and setters to my main controller, currently I'm trying to create a separate class which I can create a new instance of within my controller but it crashes when I try and use the setter.
Thank you in advance for your time, sorry if this question is as stupid as I'm sure it is.
Here's the header for my class
QuizQuestion.h
#import <Foundation/Foundation.h>
#interface QuizQuestion : NSObject {
NSString *question;
}
#property (retain) NSString* question;
#end
QuizQuestion.m
#import "QuizQuestion.h"
#implementation QuizQuestion
#synthesize question;
- (void) dealloc
{
[question release];
[super dealloc];
}
#end
And here is my controller code (i've cut some out)
#implementation Quiz2ViewController
#class QuizQuestion; // Is this correct?
- (void)viewDidLoad {
QuizQuestion *aQuestion;
//gets here fine, but crashes (the app closes) when I set question.
[aQuestion setQuestion:#"hello world"];
[super viewDidLoad];
}
#end
As well as #class I tried import "QuizQuestion.h" and I get the same issue.
You're not actually allocating an instance of the QuizQuestion class—your aQuestion variable isn’t pointing to anything in particular, so trying to send it a message, like -setQuestion:, is sending that message to... well, there’s no telling where, and sending things messages that aren’t meant for them is a surefire way to crash your app. What you need to do is this:
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
[aQuestion setQuestion:#"hello world"];
You also need to call [aQuestion release] at some point, or you’ll leak the memory associated with it.
You need to allocate space and initialize the QuizQuestion.
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
before setting the question.
I think you need to read up on some documentation before you try any more coding. Apple has several intro programming guides that are very good. The problem you are having is addressed in this section:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAllocInit.html
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
[aQuestion setQuestion:#"hello world"];
[aQuestion release];

Problem with dealloc method - 'xmlEntity' is not an Objective-C class name or alias

I am in the process of cleaning my code and testing for bugs, when I came across this build error: ['xmlEntity' is not an Objective-C class name or alias]. Here is a shorten version of my class .h file.
#interface PMXMLParser : NSXMLParser {
NSMutableDictionary *xmlEntity;
NSMutableDictionary *collectionDict;
}
#property (nonatomic, retain) NSMutableDictionary *xmlEntity;
#property (nonatomic, retain) NSMutableDictionary *collectionDict;
#end
Here is the .m file.
#implementation PMXMLParser
#synthesize xmlEntity, collectionDict
- (void) dealloc
{
// this builds correctly, with no issues.
[collectionDict release];
// 1. This works
//self.xmlEntity = nil;
// 2. This causes the build error: 'xmlEntity' is not an Objective-C class name or alias
//[xmlEntity release];
[super dealloc];
}
#end
Now to me these example 1 and 2 do the same thing, just with a little bit more work is done for number 1.
Does anyone know why I am getting this build error for number 2?
Edit: 07/30/2010 - The code presented here will compile correctly, this is just a shorten version of my whole class. But my current class does not compile. I will post the whole class later when I taken out private code.
Thanks.
Is that really your implementation? You need to have a separate #implementation section in a ".m" source file, and you need to #synthesize those properties before you can use them. Also, your dealloc function needs to go in the #implementation section. But, those issues aside, if you can use the expression self.xmlEntity but not xmlEntity it means there is a scoping issue (probably because you aren't in the #implementation section) which can be fixed by using:
[self.xmlEntity release];
That said, I would address the aforementioned issues properly.
I found out that the program I was working on included the framework libxml2.dylib. Which had an structure called xmlEntity. What lead me to the problem was the syntax color for the item xmlEntity, which was a light purple color.

iPhone dev - issue with #selector

in my app I am trying to run some code that currently exists in my applicationWillTerminate in appDelegate.
I have c/p'd the same code into the method that is currently running (verified by NSLog), but the code just doesnt seem to execute the same way.
The following code is from my applicationWillTerminate, which saves data, ready for loading next time.
[myArray makeObjectsPerformSelector:#selector(saveAllDataLeads)];
when I insert this into my DetailViewController.m (in a method that is currently active), I insert the following.
[appDelegate.myArray makeObjectsPerformSelector:#selector(saveAllDataLeads)];
The problem is that it just doesn't do the stuff in saveAllDataLeads, can someone see what is wrong? or is more information required.
Regards
in DetailViewController.h i have declared
MyAppDelegate *appDelegate;
The objects that you have added to myArray must have a selector with no parameters, named saveAllDataLeads, that is:
#interface MyObject : NSObject {
}
- (void)saveAllDataLeads;
#end
#implementation MyObject
- (void)saveAllDataLeads {
// do something
}
#end
Then, presumably somewhere you are adding instances of MyObject to myArray:
MyObject* instance = [MyObject new];
[appDelegate.myArray addObject:instance];
[instance release];