Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 2, 2008, 06: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, 06: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, 08: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 3, 2008, 12:44 AM   #4
PhoneyDeveloper
macrumors 68030
 
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, 01: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, 03: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

Similar Threads
thread Thread Starter Forum Replies Last Post
Second internal Advanced Format Drive - Won't Initialize / Format 1Tb 7K1000 OSX 10.9 ShaggyDog MacBook Pro 6 Sep 3, 2014 03:45 PM
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 11:45 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 11:42 AM
Resolved: variable arguments help (Cocoa format string) ArtOfWarfare Mac Programming 1 May 1, 2013 03:22 AM
Date from String With an Inconsistent Format harryslotwiner iPhone/iPad Programming 6 Jul 26, 2012 12:43 AM

Forum Jump

All times are GMT -5. The time now is 07:35 PM.

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

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