开发者

Iphone Text Array, Error when passing string to method. Can't explain, must be array terminator ?

I am getting NSCFString Errors passing the results of a text string converted into an array to a method that expects strings.

I have a feeling that the problem is that there is something wrong with the array conversion but i am not clever enough to work this out !!!

here's the .h file

@interface RootViewController : UIViewController <ZXingDelegate> {
IBOutlet UITextView *resultsView;
NSString *resultsToDisplay;

IBOutlet UITextField *ItemNo;
NSString *ItemNoToDisplay;

IBOutlet UITextField *VariantCode;
NSString *VariantCodeToDisplay;

IBOutlet UITextField *Description;
NSString *DescriptionToDisplay;

IBOutlet UITextField *Qty;
NSString *QtyToDisplay;

}

@property (nonatomic, retain) IBOutlet UITextView *resultsView;
@property (nonatomic, copy) NSString *resultsToDisplay;

@property (nonatomic, retain) IBOutlet UITextField *ItemNo;
@property (nonatomic,copy) NSString *ItemNoToDisplay;

@property (nonatomic, retain) IBOutlet UITextField *VariantCode;
@property (nonatomic,copy) NSString *VariantCodeToDisplay;

@property (nonatomic, retain) IBOutlet UITextField *Description;
@property (nonatomic,copy) NSString *DescriptionToDisplay;

@property (nonatomic, retain) IBOutlet UITextField *Qty;
@property (nonatomic,copy) NSString *QtyToDisplay;

in the .m file i am doing this, the code is based on the Zxing Barcode scanning scan test application.

The barcode i am scanning has a string separated by ;

- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
self.resultsToDisplay = result;
if (self.isViewLoaded) 
{
    //This is where the result comes back from the scanner.
    //Need to use this to add items to a b开发者_运维百科asket etc
    //This is where we can create a new basket screen...

    //NSString *myString = @"This is a test";
    NSArray *myArray = [result componentsSeparatedByString:@";"];

    ItemNoToDisplay = [myArray objectAtIndex:0];
    [ItemNo setText:ItemNoToDisplay];
    [ItemNo setNeedsDisplay];

    VariantCodeToDisplay = [myArray objectAtIndex:1];
    [VariantCode setText:VariantCodeToDisplay];
    [VariantCode setNeedsDisplay];


    DescriptionToDisplay = [myArray objectAtIndex:2];
    [Description setText:DescriptionToDisplay];
    [Description setNeedsDisplay];

    [resultsView setText:resultsToDisplay];
    [resultsView setNeedsDisplay];

    }
   [self dismissModalViewControllerAnimated:NO];
}

I then have a button on the screen that the user uses to pass the data to a simple method.

-(int)AddItemToBasket:(NSString *)ItemNo:(int)QtyToAdd:(NSString *)ItemDescription:(double)SalesPrice:(NSString *)DisplayPrice;

the app runs, the user scans the code, the window gets filled in correctly, there are some fields to hold the split data.

when the use presses the button to call the method.

if i use what i expect to work

 Utility *sharedUtility = [Utility sharedUtility];

 [sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(DescriptionToDisplay):(0):(@"1")];

but this does

 Utility *sharedUtility = [Utility sharedUtility];

 [sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(Description.text):(0):(@"1")];

The first call always errors with bad access or not a NSCFstring type error.

It seems that the LAST part of the array has some funny char in it that the textfield handles, but the code does not. Some sort of termination issue.

I have worked around it by using the variable from the text box, but am confused as to why i can pass directly the value in the textfield i am passing to the text box ?

Help / Confused..


You are directly assigning to your string ivars instead of going through the synthesized accessors (e.g. should be self.DescriptionToDisplay = ....

This means you are assigning an autoreleased object to the ivar, which may not be around by the time you call your AddItemToBasket method.

As an aside, it is conventional to begin method names and ivar names with lower case letters, and also to name arguments in your methods, that AddItemToBasket method is very difficult to read.

In answer to your comments, it may sometimes work if the area of memory has not been re-allocated since the autorelease. But you definitely need to use the accessors, particularly the setter - this will copy and increase the retain count for you. You could directly use the ivar for the getter in this situation.


You go through all the effort of defining properties but then never use them. This means that you're not retaining the values, and the OS is deallocating them before you actually use them.

So here:

DescriptionToDisplay = [myArray objectAtIndex:2]; 

You need:

self.DescriptionToDisplay = [myArray objectAtIndex:2]; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜