How to remove a specific object from an array

doc92606

So I'm currently making a calorie tracker. In this app, you enter the recent meal you ate, and the amount of calories in that meal. Then you press the "add" button. This button will save the data to a TableView called HistoryTableViewController. To check if the person has left a textfield blank, I have 3 "if statement". One looks like this:

if ([[self.amountText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] < 1)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"You have not entered the amount of Calories!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [amountHistory removeObjectAtIndex:0];

}

There is one line of code that I know is wrong which is the

[amountHistory removeObjectAtIndex:0];

I don't necessarily want to remove the first object on the tableview, I just want to make sure that the item doesn't get added.

Thanks in advance.

EDIT:

I add an object to the array at the top of the if statements:

 total+= self.amountText.text.intValue;
 self.totalLabel.text = [NSString stringWithFormat:@"%d", total];
NSNumber *calorieNumber = [NSNumber numberWithInt:self.amountText.text.intValue];
NSString *foodString = nameOfTheFood.text;
NSString *historyString = [NSString stringWithFormat:@"%@ Calories in %@",       calorieNumber, foodString];
[amountHistory addObject:historyString];
Paul.s

Sounds like you are thinking about it from the wrong angle.

Instead of adding the item to your array and then removing it if it is invalid, you should ensure the item is only added when it is valid.

So in your method you would only add the item at the end, after all validation has passed. You would add early return's in your if statements if the any validation fails so that you don't reach the code that adds the item to the array

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove a specific Object from an array of Objects, by object's property?

From Dev

Remove specific element from array of specifc object

From Dev

How to remove a specific object from a List<T>

From Dev

How to remove object with specific name from ArrayList

From Dev

How to remove a specific object from a List<T>

From Dev

How to remove specific element from array with RactiveJs

From Dev

How to remove a specific array from an ArrayList

From Dev

Remove specific object from array and display remaining in a table

From Dev

How to remove properties from an object array?

From Dev

How to remove an object from an array in Immutable?

From Dev

How to remove an object from array of tuples

From Dev

How to remove key from Array of Object

From Dev

How to remove array object from for loop

From Dev

How to remove "integer" array from object in JavaScript

From Dev

How to remove key from Array of Object

From Dev

How to remove properties from an object array?

From Dev

How to remove an object from an array in Perl

From Dev

How to remove an empty object from an array?

From Dev

How to remove elements from nested object array?

From Dev

Remove object from array

From Dev

Remove an object from an array

From Dev

How to remove single object in array from multiple matching object

From Dev

javascript - how to remove a specific value from a property in an object?

From Dev

how to delete a specific object from array object in MongoDB

From Dev

How can I remove object in one array from another array?

From Dev

How can I remove object in one array from another array?

From Dev

Remove specific item from array

From Dev

Remove specific numbers from array

From Dev

How to access specific value from a nested array within an object array?

Related Related

  1. 1

    How to remove a specific Object from an array of Objects, by object's property?

  2. 2

    Remove specific element from array of specifc object

  3. 3

    How to remove a specific object from a List<T>

  4. 4

    How to remove object with specific name from ArrayList

  5. 5

    How to remove a specific object from a List<T>

  6. 6

    How to remove specific element from array with RactiveJs

  7. 7

    How to remove a specific array from an ArrayList

  8. 8

    Remove specific object from array and display remaining in a table

  9. 9

    How to remove properties from an object array?

  10. 10

    How to remove an object from an array in Immutable?

  11. 11

    How to remove an object from array of tuples

  12. 12

    How to remove key from Array of Object

  13. 13

    How to remove array object from for loop

  14. 14

    How to remove "integer" array from object in JavaScript

  15. 15

    How to remove key from Array of Object

  16. 16

    How to remove properties from an object array?

  17. 17

    How to remove an object from an array in Perl

  18. 18

    How to remove an empty object from an array?

  19. 19

    How to remove elements from nested object array?

  20. 20

    Remove object from array

  21. 21

    Remove an object from an array

  22. 22

    How to remove single object in array from multiple matching object

  23. 23

    javascript - how to remove a specific value from a property in an object?

  24. 24

    how to delete a specific object from array object in MongoDB

  25. 25

    How can I remove object in one array from another array?

  26. 26

    How can I remove object in one array from another array?

  27. 27

    Remove specific item from array

  28. 28

    Remove specific numbers from array

  29. 29

    How to access specific value from a nested array within an object array?

HotTag

Archive