[NSDecimalNumber Length]:发送到实例的无法识别的选择器
[NSDecimalNumber Length]:unrecognized selector sent to instance
我在简单的 TableViewController 项目上遇到错误,因为看起来我试图将 NSDecimalNumber
对象强制放入名为 CheckValue.text
的标签的文本中作为 cellForRowAtIndexPath
方法。
说实话,我正在尝试这样做,因为我需要 NSDecimalNumber
的值显示在 TableCell 上。有人能帮忙吗,因为我觉得我快要中弹了?
这是来自 TableViewController.m
的代码
#import "TableViewController.h"
#import "TableCell.h"
@interface TableViewController ()
{
NSMutableData *webData;
NSURLConnection *connection;
NSMutableArray *array;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Get an array from web and Populate Check Arrays with New Data as above ...
[self GetDatabaseData];
}
-(void)GetDatabaseData{
NSURL *blogURL = [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
//NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error];
NSArray *TransactionArray = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error];
NSDecimalNumber *check_total = [NSDecimalNumber zero];
NSString *current_check;
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < TransactionArray.count; i++)
{
NSDictionary *jsonElement = TransactionArray[i];
// Accumulate if this is part of the same check.
NSDecimalNumber *line_total = [NSDecimalNumber decimalNumberWithString:jsonElement[@"tran_value"]];
// Decide if this is Element Belong to the Last Check number.
if (jsonElement[@"tran_check"] == current_check){
check_total = line_total; // Reset Check Total to be the Value of this Line on NEW Checks
}
else{
check_total = [check_total decimalNumberByAdding:line_total]; //Add this Line Value to the Running Check Total
}
current_check = jsonElement[@"tran_check"]; // Make this Check number the Current Check for Totalizing.
_Checknumbers = @[jsonElement[@"tran_check"]];
_CheckTime = @[jsonElement[@"tran_hour"]];
_CheckValue = @[check_total];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _Checknumbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int row = [indexPath row];
cell.CheckLabel.text = _Checknumbers[row];
cell.CheckValue.text = _CheckValue[row];
cell.CheckTime.text = _CheckTime[row];
return cell;
}
@end
NSDecimalNumber
继承自 NSNumber
因此 NSNumberFormatter
格式化程序可用于获取您需要的字符串表示形式。
这是一个示例,可以根据您的需要进行更改:
- (NSString *)formatNumber:(NSDecimalNumber *)number {
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setMinimumFractionDigits:2];
numberFormatter.usesGroupingSeparator = YES;
return [numberFormatter stringFromNumber:number];
}
示例:
number returns
12456.3 12,456.30
12 12.00
12.3456 12.35
我在示例计算器应用程序中使用了它。
如果formatNumber
方法添加到TableViewController
class:
cell.CheckValue.text = [self formatNumber:_CheckValue[row]];
为了更容易调试(总是一件好事)
NSDecimalNumber *checkValueDecimal = _CheckValue[row];
NSString *checkValueString = [self formatNumber:_ checkValueDecimal];
cell.label.text = numberString;
这样可以检查每个步骤,编译器会在发布版本上优化它。
您可以使用 NSDecimalNumber 值格式化字符串
cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]
UIlabel显示的对象是classNSString,你传入的参数是NSDecimalNumber,当你设置文本时检查传入的值包括长度(Lenght方法)因为对象不支持这个方法——报错是抛出。解决方案 - 将 NSDecimalNumber 转换为 NSString。
例如:
cell.CheckValue.text = [_CheckValue[row] stringValue];
我在简单的 TableViewController 项目上遇到错误,因为看起来我试图将 NSDecimalNumber
对象强制放入名为 CheckValue.text
的标签的文本中作为 cellForRowAtIndexPath
方法。
说实话,我正在尝试这样做,因为我需要 NSDecimalNumber
的值显示在 TableCell 上。有人能帮忙吗,因为我觉得我快要中弹了?
这是来自 TableViewController.m
的代码#import "TableViewController.h"
#import "TableCell.h"
@interface TableViewController ()
{
NSMutableData *webData;
NSURLConnection *connection;
NSMutableArray *array;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Get an array from web and Populate Check Arrays with New Data as above ...
[self GetDatabaseData];
}
-(void)GetDatabaseData{
NSURL *blogURL = [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
//NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error];
NSArray *TransactionArray = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error];
NSDecimalNumber *check_total = [NSDecimalNumber zero];
NSString *current_check;
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < TransactionArray.count; i++)
{
NSDictionary *jsonElement = TransactionArray[i];
// Accumulate if this is part of the same check.
NSDecimalNumber *line_total = [NSDecimalNumber decimalNumberWithString:jsonElement[@"tran_value"]];
// Decide if this is Element Belong to the Last Check number.
if (jsonElement[@"tran_check"] == current_check){
check_total = line_total; // Reset Check Total to be the Value of this Line on NEW Checks
}
else{
check_total = [check_total decimalNumberByAdding:line_total]; //Add this Line Value to the Running Check Total
}
current_check = jsonElement[@"tran_check"]; // Make this Check number the Current Check for Totalizing.
_Checknumbers = @[jsonElement[@"tran_check"]];
_CheckTime = @[jsonElement[@"tran_hour"]];
_CheckValue = @[check_total];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _Checknumbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int row = [indexPath row];
cell.CheckLabel.text = _Checknumbers[row];
cell.CheckValue.text = _CheckValue[row];
cell.CheckTime.text = _CheckTime[row];
return cell;
}
@end
NSDecimalNumber
继承自 NSNumber
因此 NSNumberFormatter
格式化程序可用于获取您需要的字符串表示形式。
这是一个示例,可以根据您的需要进行更改:
- (NSString *)formatNumber:(NSDecimalNumber *)number {
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setMinimumFractionDigits:2];
numberFormatter.usesGroupingSeparator = YES;
return [numberFormatter stringFromNumber:number];
}
示例:
number returns 12456.3 12,456.30 12 12.00 12.3456 12.35
我在示例计算器应用程序中使用了它。
如果formatNumber
方法添加到TableViewController
class:
cell.CheckValue.text = [self formatNumber:_CheckValue[row]];
为了更容易调试(总是一件好事)
NSDecimalNumber *checkValueDecimal = _CheckValue[row];
NSString *checkValueString = [self formatNumber:_ checkValueDecimal];
cell.label.text = numberString;
这样可以检查每个步骤,编译器会在发布版本上优化它。
您可以使用 NSDecimalNumber 值格式化字符串
cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]
UIlabel显示的对象是classNSString,你传入的参数是NSDecimalNumber,当你设置文本时检查传入的值包括长度(Lenght方法)因为对象不支持这个方法——报错是抛出。解决方案 - 将 NSDecimalNumber 转换为 NSString。 例如:
cell.CheckValue.text = [_CheckValue[row] stringValue];