PDA

View Full Version : Format NSInteger into a string?




jchildress
Sep 2, 2008, 05:45 PM
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:
[cell setText:(@"%d", appDelegate.myInt)];

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

My main app creates the NSInteger property like so:
@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
Sep 2, 2008, 05:47 PM
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
Sep 2, 2008, 07:40 PM
Should I define it like this:
@interface MainAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
NSInteger myInt;
}
@property (assign) NSInteger myInt;

@end

or like this:
@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
Sep 2, 2008, 11:44 PM
Try it like this

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

assuming that your accessor is set up correctly.

jchildress
Sep 3, 2008, 12:30 AM
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
Sep 3, 2008, 02:17 AM
Your interface should be like this


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

@end


The implementation should like this

@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)


int retVal = UIApplicationMain(argc, argv, nil, @"MainAppDelegate");