找到你要的答案

Q:[NSDecimalNumber Length]:unrecognized selector sent to instance

Q:[ nsdecimalnumber长度]:无法识别的选择发送给实例

I am getting an error when on a simple TableViewController Project as it looks like I am trying to force an NSDecimalNumber object into the text of a label called CheckValue.text as part of the cellForRowAtIndexPath method.

And truth be told, I am trying to do this as I need to value of the NSDecimalNumber to be displayed on the TableCell. Can anyone help as I feel like I am close to a bullet here?

Here is the code from the 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

我得到一个错误时,一个简单的了TableViewController项目看起来我试图迫使nsdecimalnumber对象到一个标签的文本称为checkvalue.text是cellForRowAtIndexPath方法部分。

说实话,我想这样做,因为我需要的nsdecimalnumber被显示在表元值。有谁能帮我,因为我觉得我接近子弹在这里?

下面的代码是从了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
answer1: 回答1:

You can format a string with the NSDecimalNumber value

cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]

你可以用nsdecimalnumber值的字符串格式

cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]
answer2: 回答2:

UIlabel displays objects of class NSString, you passed as a parameter NSDecimalNumber, when you set the text is checked incoming values including length (Lenght method) because the object does not support this method - an error is thrown. Solution - convert NSDecimalNumber to NSString. For example:

cell.CheckValue.text = [_CheckValue[row] stringValue];

UIlabel displays objects of class NSString, you passed as a parameter NSDecimalNumber, when you set the text is checked incoming values including length (Lenght method) because the object does not support this method - an error is thrown. Solution - convert NSDecimalNumber to NSString. For example:

cell.CheckValue.text = [_CheckValue[row] stringValue];
answer3: 回答3:

NSDecimalNumber inherits from NSNumber so NSNumberFormatter formatter can be used to get the string representation you need.

Here is an example, change to suit your needs:

- (NSString *)formatNumber:(NSDecimalNumber *)number {
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
    [numberFormatter setMinimumFractionDigits:2];
    numberFormatter.usesGroupingSeparator = YES;

    return [numberFormatter stringFromNumber:number];
}

Examples:

number     returns
12456.3  12,456.30  
12           12.00  
12.3456      12.35  

I used that in a sample calculator app.

If the formatNumber method is added to the TableViewController class:

cell.CheckValue.text = [self formatNumber:_CheckValue[row]];

For easier debugging (always a good thing)

NSDecimalNumber *checkValueDecimal = _CheckValue[row];
NSString *checkValueString = [self formatNumber:_ checkValueDecimal];
cell.label.text = numberString;

This way one can examine each step, the compiler will optimize this away on release builds.

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类:

cell.CheckValue.text = [self formatNumber:_CheckValue[row]];

为了更容易的调试(总是一件好事)

NSDecimalNumber *checkValueDecimal = _CheckValue[row];
NSString *checkValueString = [self formatNumber:_ checkValueDecimal];
cell.label.text = numberString;

这种方式可以检查每一步,编译器将优化这个版本。

objective-c  unrecognized-selector  nsdecimalnumber