Question

how to store NSString to Database as INTEGER.

When update database, the app get Abend.

①User enter a number into UITextField *numTextField by UIPickerView[1~10]
②convert numTextField.text into int num_int by method intValue.
③store num to DB
④convert book.num into NSString *num_text by method stringValue.
⑤output num_text to numTextField and cell.label3.text

I have this code.

Book class operate DB.

Book.m

#define SQL_INSERT @"INSERT INTO books1 (day, title, time, num) VALUES (?, ?, ?, ?);"

- (Book*)add:(Book *)book{
 FMDatabase* db = [self getConnection];
 [db open];
 [db setShouldCacheStatements:YES];

 if( [db executeUpdate:SQL_INSERT, book.day, book.title, book.time, book.num] {
  book.bookId = [db lastInsertRowId];
  }

 else {
  book = nil;
 }

 [db close];

 return book;
}

EditViewController.m

-(void)viewDidLoad{
 if( self.book )
    {
        _titleTextField.text = self.book.title;
        _dayTextField.text = self.book.day;
        _timeTextField.text = self.book.time;

        int num_int = book.num;
        NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int];
        NSString *num_text = [num_n stringValue];
        cell.label3.text = num_text;
    }
 }

- (IBAction)saveData:(id)sender
{    
    Book* newBook = [[Book alloc] init];
    newBook.bookId    = self.book.bookId;
    newBook.title     = _titleTextField.text;
    newBook.day    = _dayTextField.text;
    newBook.time    = _timeTextField.text;

    NSString *num_text = _numTextField.text;
    int num_int = [num_text intValue];
    newBook.num = num_int;

if( self.book ){
  [self.delegate editBookDidFinish:self.book newBook:newBook];
 }
else{
  [self.delegate addBookDidFinish:newBook];
 }
}

TableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Book* book = [self bookAtIndexPath:indexPath];
    cell.label1.text = book.title;
    cell.label2.text = book.time;

    int num_int = book.num;
    NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int];
    NSString *num_text = [num_n stringValue];
    cell.label3.text = num_text;

    return cell;
}

the cell.label3.text outputs “0” in entering anything to numTextField.text.
I’d like to store num_text to database as INTEGER, because num will be added and subtracted by a button.

Any idea on how I could fix it? Thanks in advance.

Was it helpful?

Solution

try this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        Book* book = [self bookAtIndexPath:indexPath];
        cell.label1.text = book.title;
        cell.label2.text = book.time;

        int num_int = book.num;
        NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int];
        NSString *num_text = [NSString stringWithFormate:@"%@",num_n]
        cell.label3.text = num_text;

    return cell;
}

OTHER TIPS

[NSString stringWithFormat:@"%d", count];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top