-3

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rob4236
  • 375
  • 1
  • 7
  • 12
  • Possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Oct 07 '15 at 23:06
  • Hi Hot Licks, I did manage to find the Problem with the Line of Code, and knew what had to be done, just didn't know the Syntax for this conversion. – Rob4236 Oct 08 '15 at 14:34

3 Answers3

0

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

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];
AshCenso
  • 11
  • 3
  • I understand now thanks. You would think that this type of passing of types would be picked up and the Build Stage and not leave it to the Runtime ??? – Rob4236 Oct 08 '15 at 14:37
  • You can set type of objects in array and take warning, but you cannot pass this in build stage – AshCenso Oct 08 '15 at 16:07
-1

You can format a string with the NSDecimalNumber value

cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]
Boris
  • 11,373
  • 2
  • 33
  • 35
  • Thanks for this Boris, will check code later when I get home. Wandered if you may be able to help with the next question which is : As you can see, I am Looping through a NSArray called TransactionArray using [i], however within the loop you can see I am populating 3 Other NSArrays from Elements of the JSONElement. Like This: _Checknumbers = @[jsonElement[@"tran_check"]]; _CheckTime = @[jsonElement[@"tran_hour"]]; _CheckValue = @[check_total]; This of course means that each of the NSArrays only contains the Last Value of the Elements from the TransactionArray.... – Rob4236 Oct 08 '15 at 14:29
  • How can I ADD a New Value to each Array within the Loop instead of Replacing the Value of the Array to just this 1 Object ? – Rob4236 Oct 08 '15 at 14:33