Iphone 文本数组,将字符串传递给方法时出错。无法解释,一定是数组终止符吗?
我收到 NSCFString 错误,将转换为数组的文本字符串的结果传递给需要字符串的方法。
我有一种感觉,问题是数组转换有问题,但我不够聪明,无法解决这个问题!
这是我正在执行此操作的.m 文件中的.h 文件
@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;
,该代码基于Zxing 条码扫描扫描测试应用程序。
我正在扫描的条形码有一个以 ; 分隔的字符串。
- (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 basket 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];
}
然后,我在屏幕上有一个按钮,用户可以使用该按钮将数据传递给一个简单的方法。
-(int)AddItemToBasket:(NSString *)ItemNo:(int)QtyToAdd:(NSString *)ItemDescription:(double)SalesPrice:(NSString *)DisplayPrice;
应用程序运行,用户扫描代码,窗口被正确填充,有一些字段用于保存分割数据。
当用户按下按钮时调用该方法。
如果我使用我期望的工作
Utility *sharedUtility = [Utility sharedUtility];
[sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(DescriptionToDisplay):(0):(@"1")];
,但这确实
Utility *sharedUtility = [Utility sharedUtility];
[sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(Description.text):(0):(@"1")];
第一次调用总是出现访问错误或不是 NSCFstring 类型错误的错误。
数组的最后部分似乎有一些文本字段处理的有趣字符,但代码没有。某种终止问题。
我已经通过使用文本框中的变量解决了这个问题,但我很困惑为什么我可以直接传递我传递到文本框的文本字段中的值?
帮助/困惑..
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 basket 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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您直接分配给字符串 ivar,而不是通过合成访问器(例如应该是 self.DescriptionToDisplay = ...。
这意味着您正在将自动释放的对象分配给 ivar,这可能不是 顺便说一句
,方法名称和 ivar 名称通常以小写字母开头,并且在方法中命名参数,这样 AddItemToBasket 方法很难阅读
。回答你的评论,如果自自动释放以来内存区域没有被重新分配,它有时可能会起作用,但你肯定需要使用访问器,特别是设置器 - 这将为你复制并增加保留计数。在这种情况下,直接使用 ivar 作为 getter。
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:
You need: