1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Format NSInteger into a string?

Discussion in 'iPhone/iPad Programming' started by jchildress, Sep 2, 2008.

  1. macrumors member

    #1
    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.
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    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.
     
  3. macrumors member

    #3
    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?
     
  4. macrumors 68030

    PhoneyDeveloper

    #4
    Try it like this

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

    assuming that your accessor is set up correctly.
     
  5. macrumors member

    #5
    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?
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    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");
    
     

Share This Page