2

I have a problem for my xCode application.

My application is about record outcome. Thus the items values should be positive. I try to prevent record negative or zero value while choosing an item, but my code did not function.

This is my code to record an item

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Outcome Item Value" message:@"Please enter an outcome item value" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

[alert show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0) {

  }
  else
  {
    OutcomeItem* newOutcomeItem = [NSEntityDescription insertNewObjectForEntityForName:@"OutcomeItem" inManagedObjectContext:self.managedObjectContext];


    newOutcomeItem.outcomeName = self.selectedOutcomeItem.outcomeName;
    newOutcomeItem.outcomeValue = [NSNumber numberWithDouble:[[alertView textFieldAtIndex:0].text doubleValue]];


    if (newOutcomeItem.outcomeValue <= 0) {

        NSLog(@"Invalid Value");
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {

        [self.delegate addOutcomeItem:newOutcomeItem];
        [self.navigationController popViewControllerAnimated:YES];
    }
  }
}

my OutcomeItem.h is

#import<Foundation/Foundation.h>
#import<CoreData/CoreData.h>
@class OutcomeRecord;
@interface OutcomeItem : NSManagedObject
@property (nonatomic, retain) NSString *outcomeName;
@property (nonatomic, retain) NSNumber *outcomeValue;
@property (nonatomic, retain) OutcomeRecord *outcomeRecord;
@end;

OutcomeItem.m is

#import "OutcomeItem.h"
#import "OutcomeRecord.h"

@implementation OutcomeItem
@dynamic outcomeName;
@dynamic outcomeValue;
@dynamic outcomeRecord;
@end

Add outcome item method is

-(void)addOutcomeItem:(OutcomeItem *)outcomeItem

{ [self.currentOutcomeRecord addOutcomeItemsObject:outcomeItem]; self.outcomeRecordItemList = [self.currentOutcomeRecord.outcomeItems allObjects];

[self.tableView reloadData];
NSError* error;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Could not save outcome item insertion:\n%@", error.userInfo);
}

}

1 Answers1

1

I found the issue. You need cast to a doubleValue from NSNumber.

 NSNumber *outcomeValueX = [NSNumber numberWithDouble:[[alertView textFieldAtIndex:0].text doubleValue]];

 if (outcomeValueX.doubleValue <= 0) {  
        NSLog(@"Invalid Value");
 } 

Ideally I would try to validate it with a UITextFieldDelegate to your UITextField, and implement textField:shouldChangeCharactersInRange:replacementString.

See here: How to Test Character Input to UITextField as the User Enters Characters and Prevent Invalid Characters

I would also not add a new outcomeItem until the value is validated.

Community
  • 1
  • 1
Andy B
  • 629
  • 8
  • 15
  • 1
    so, `NSString` -> `double` -> `NSNumber` -> `double`? Why `NSNumber`? – Sulthan Jun 16 '14 at 13:56
  • Exactly! Funny to miss that! ;) Something like the following would be enough: double myCoolDouble = [alertView textFieldAtIndex:0].text doubleValue] – Andy B Jun 16 '14 at 13:58