0

I have an object: indivOrder:

@interface indivOrderDetails : NSObject{

    NSNumber* shirtNumber;
    NSNumber* pantsNumber;
    NSNumber* jacketNumber;
    NSNumber* laundryNumber;
    NSNumber* blouseNumber;
    NSNumber* blazerNumber;
    NSNumber* skirtNumber;
    NSNumber* suitNumber;
    NSString* pickUpOrDropOff;
    NSString* pickUpFrom;
    NSNumber* totalOrderPrice;
}

They're all given the interface of

 @property (nonatomic, retain) NSNumber* propertyName

I have three steps.

First I retrieve the data from a text field:

 shirtNumber = [self convertStringToNumber:shirtField.text];

Second, I use this convertStringToNumber method.

-(NSNumber*) convertStringToNumber:(NSString*)stringToConvert
{

    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *myNumber = [f numberFromString:stringToConvert];

    return myNumber;

}

Then I assign that value to my object variable.

 orderDetails.shirtNumber = shirtNumber;

But the only value I'm coming back with when I try to access the orderDetails.shirtNumber variable is zero. The shirtNumber is coming back with the correct value from the ViewController.

halfer
  • 19,824
  • 17
  • 99
  • 186
SonOfSeuss
  • 400
  • 5
  • 18
  • try remove all ivars, they are not required now and tent to lead bugs. – Bryan Chen Nov 19 '13 at 02:18
  • Are you sure that `shirtField.text` is not nil? If `shirtField` is an IBOutlet for a text field created in a xib, is the text field hooked up to the outlet? – NSAdam Nov 19 '13 at 02:23
  • NSAdam -- it's definitely not nil. @BryanChen What do you mean ivars? My understanding of an ivar is an instance variable. – SonOfSeuss Nov 19 '13 at 02:27
  • yes. remove all your instance variable declaration and only use property. e.g. `self.shirtNumber` instead of `shirtNumber` – Bryan Chen Nov 19 '13 at 02:29
  • Not only that, but now the system gives you an ivar of _name (where name is the property), so when you access "name" you get nil all the time!!! – David H Nov 19 '13 at 02:32
  • Put some `NSLog()` statements in to see what is happening±—or check the progress in Xcode. – zaph Nov 19 '13 at 02:44
  • @BryanChen I've tried `orderDetails.shirtNumber = [self convertStringToNumber:shirtField.text]` and still got the same results. This way didn't use any instance variables at all. – SonOfSeuss Nov 19 '13 at 02:47
  • Have you tried logging myNumber? Try using `NSNumber *myNumber = [NSNumber numberWithInt:[stringToConvert intValue]]`; – Suhas Nov 19 '13 at 02:50
  • How are you instantiating orderDetails, and where are you doing that? Is some of the code you posted in one class, and some in another? You should clarify what code is in what controller (if there is more than one). – rdelmar Nov 19 '13 at 03:04
  • Okay @rdelmar. The `indivOrderDetails` is the class name. It's in a separate file. The instantiation of the `indivOrderDetails` is in the .h file of the view controller where these actions are taking place. I synthesize the object in the .m file in the view controller itself. – SonOfSeuss Nov 19 '13 at 03:21
  • 1
    You don't instantiate things in a .h file. Your comment still doesn't clarify much. Show the code where you think you're instantiating orderDetails. Edit you question to make it clear what code is in what controller. – rdelmar Nov 19 '13 at 05:29

1 Answers1

0

Your problem likely lies here:

shirtNumber = [self convertStringToNumber:shirtField.text];

Look at that to which you're assigning. It's one of the ivars you declared. That ivar, despite what you may think, is not the backing for your property of the same name. The backing instead would be

_shirtNumber

since you don't appear to have synthesized (i.e., used the @synthesize directive) any accessors.

As a result, _shirtNumber and shirtNumber are two distinct entities, which means that when you attempt to access

orderDetails.shirtNumber

of course it's going to be nil. You never put anything in it.

So you have two choices: Either use the property name that's prefixed with an underscore

_shirtNumber = [self convertStringToNumber:shirtField.text];

or use @synthesize to set up some accessors, in which case you'd do this

self.shirtNumber = [self convertStringToNumber:shirtField.text];

or this (if you like the old form)

[self setShirtNumber:[self convertStringToNumber:shirtField.text]];

Guess that's three choices. Anyway, it's a subtle, classic 'gotcha.' If you want further background for this, you can find it in this very excellent explanation elsewhere on Stack Overflow.

Good luck to you in your endeavors.

Community
  • 1
  • 1
Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47