0

Hi i have a simple question, what is the best choice for this method:

Class Product

- (NSMutableArray*)getAllProductsByCategory:(NSInteger)categoryID
{
    .................
    sqlite3_bind_int(statement, 1, categoryID);
    .................
}

file.h

@interface ListViewController : UITableViewController {
    NSInteger *number;
}

file.m

// with NSInteger: incompatible pointer to integer conversion
self.listArray = [dbAccess getAllProductsByCategy:number];

What is the best choice for passed a simple integer like parameter to method: NSInteger, NSNumber or simple int??

Maurizio
  • 89
  • 2
  • 15

3 Answers3

0

It is best to use NSInteger. There are some things that are optimized for you when using this depending on what processor architecture you are using. See this question for more information: When to use NSInteger vs. int

Community
  • 1
  • 1
dtuckernet
  • 7,817
  • 5
  • 39
  • 54
  • Ok, thk. One question: i must set a property for nsinteger? @property (retain,nonatomic) NSInteger *number; – Maurizio Jan 18 '12 at 14:13
  • It depends on what you are using it for. You certainly can set a property for it, but since it isn't an object - it wouldn't be 'retain' just nonatomic. – dtuckernet Jan 18 '12 at 14:16
  • It would be `assign` instead of `retain` if you wanted to be specific. – deanWombourne Jan 18 '12 at 14:19
0

NSInteger - it's just typedef of int. NSNumber - is normal Objective-C class. If you would call methods by usual way: [self method:myInt], you can use what you want, but if you will perform selector with delay or in another thread i must use NSNumber because it's impossible to send not-class object for selector.
But usually I'm use NSInteger or NSUInteger (unsigned int) in all code with Cocoa framework and int, unsigned int in code written with C language.

OdNairy
  • 540
  • 4
  • 15
  • If i use NSInteger how to convert this for my method: self.listArray = [dbAccess getAllProductsByCategy:number]; Incompatible pointer to integer conversion – Maurizio Jan 18 '12 at 14:18
  • `NSNumber* myNumber = [NSNumber numberWithNSInteger:number];` – OdNairy Jan 18 '12 at 14:36
0

NSNumbers are pretty useless for anything except storing in arrays or dictionaries, where you have to use an object. You can't do math with them and they use more memory and processing time to generate and manipulate than regular integers. NSInteger is a good choice.

You probably don't want NSInteger to be a pointer as it's not an object, it's just another word for int. So instead of

NSInteger *number;

Write

NSInteger number;
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103