Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jchildress

macrumors member
Original poster
Jul 9, 2008
49
0
This is probably a very simple problem that I just don't understand due to my inexperience with objective-C, but I'm basically trying to format a saved NSInteger into the text of a tableview's cell.

I'm trying to do it like this:
Code:
[cell setText:(@"%d", appDelegate.myInt)];

I've create a reference to my main app like:
Code:
MainAppDelegate *appDelegate = (MainAppDelegate *)[[UIApplication sharedApplication] delegate];

My main app creates the NSInteger property like so:
Code:
@interface MainAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
	NSInteger myInt;
}

@property NSInteger myInt;

@end

Every time I run the app it crashes and I get a warning that says:
"warning: passing argument 1 of 'setText:' makes pointer from integer without a cast."

What am I doing wrong here? Am I defining my property wrong in the mainAppDelegate? Do I need to make it a pointer? Assign it as (nonatomic, retain)?

I've successfully done this before with other objects. But I seem to be doing something wrong when trying to pull the int from my main app. Sorry if this is so simple but I'm pretty confused here.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
NSIntegers are not objects. If you declare them as properties and synthesize the accessors you should declare the property as assign not retain, just like you would with an int property.
 

jchildress

macrumors member
Original poster
Jul 9, 2008
49
0
Should I define it like this:
Code:
@interface MainAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
	NSInteger myInt;
}
@property (assign) NSInteger myInt;

@end

or like this:
Code:
@interface MainAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
	NSInteger *myInt;
}
@property (assign) NSInteger *myInt;

@end

All I want to do is access this int from any view controller I have active in my app. So I thought if I assigned it to the main app delegate, I could access it from anywhere by creating a shared app delegate of my main app. Is there a better way to do this?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Try it like this

cell.text = [NSString stringWithFormat:mad:"%d", appDelegate.myInt];

assuming that your accessor is set up correctly.
 

jchildress

macrumors member
Original poster
Jul 9, 2008
49
0
I must not have my accessor setup properly, because non of these methods are working. How would I set it up propery Can somebody write a quick code example of how to access the int thru the shared appDelegate?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Your interface should be like this

Code:
@interface MainAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
	NSInteger myInt;
}
@property (assign) NSInteger myInt;

@end

The implementation should like this
Code:
@implementation MainAppDelegate

@synthesize myInt

@end

And most importantly they app delegate has to be created, either in you main nib (if you have one) or in main.m like this (alter the existing line)

Code:
int retVal = UIApplicationMain(argc, argv, nil, @"MainAppDelegate");
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.