Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 2, 2008, 05:45 PM   #1
jchildress
macrumors member
 
Join Date: Jul 2008
Format NSInteger into a string?

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.
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 2, 2008, 05:47 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Sep 2, 2008, 07:40 PM   #3
jchildress
Thread Starter
macrumors member
 
Join Date: Jul 2008
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?
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 2, 2008, 11:44 PM   #4
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Try it like this

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

assuming that your accessor is set up correctly.
PhoneyDeveloper is offline   0 Reply With Quote
Old Sep 3, 2008, 12:30 AM   #5
jchildress
Thread Starter
macrumors member
 
Join Date: Jul 2008
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?
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 3, 2008, 02:17 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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");
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
ascii text from a URL into a string? jerrywickey iPhone/iPad Programming 25 Apr 28, 2009 10:49 PM
format numbers in a string coolman iPhone/iPad Programming 0 Sep 24, 2008 06:03 PM
Convert a double into a string ricku425 Mac Programming 6 Feb 20, 2008 01:42 PM
convert an array of ints - into a string with semicolon in between. celia Mac Programming 20 Jun 26, 2007 03:41 PM
Convert Unicode text into a string semaja2 Mac Programming 2 May 22, 2006 05:24 AM


All times are GMT -5. The time now is 09:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC