PDA

View Full Version : Need To Get Value From Core Data textField Int and add + 1 to Integer




KieferThomas
Mar 21, 2013, 10:26 AM
Hi All,
This is the code I have so far. What I need to do is retrieve RecId conents which contains a integer.

- (IBAction)myButton:sender
{

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEEE, MMMM d, yyyy -- h:mm a"];
NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]];

int myresult = [recid.stringValue intValue]; <<<--- This what I am using to try and return recId from my Entity. This only returns 0.

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *entity = [NSEntityDescription
insertNewObjectForEntityForName:@"Entry"
inManagedObjectContext:context];

[arrayController addObject:entity];

int entityCount = 0;
NSEntityDescription *myentity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:myentity];
[fetchRequest setIncludesPropertyValues:YES];
[fetchRequest setIncludesSubentities:NO];

NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest: fetchRequest error: &error];
if(error == nil){
entityCount = count;
}

int myRowCount = [NSNumber numberWithInt:count];

myresult + 1; <<<--- Then add 1 here to recId.
[entity setValue:myreslut forKey:@"recId"]; <<<--- Set the new recId for my new Entity from myresult; myresult is still returning 0 here...
[entity setValue:dateTime forKey:@"date"];
[textField becomeFirstResponder];
}

I need to know what I am missing to get the last recId from my core data attribute entered that is in the database.

Can anyone point me in the direction or give a proper example of how to retrieve the last Entity with the attribute recId to insert into my new Entity...?

Thanks In Advance,
Kiefer



lee1210
Mar 21, 2013, 10:40 AM
+ is a binary operator. It has no effect on its operands, and evaluates to their sum.

= is a binary operator. It assigns the value of the right operand to the left operand. It evaluates to the right operand (which is also the new value of the left operand).

+= is a binary operator. It sets the value of its left operand to the sum of its operands, and evaluates to the new value of the left operand (sum of original operands).

x + y
Will have no effect on the world. It evaluates to the sum of x and y, but nothing is being done with this.

x = x + y
Will set x to the sum of x and y. It also evaluates to the sum of x and y.

x += y
Will set x to the sum of x and y. It also evaluates to the sum of x and y.

So all of these evaluate to the sum of x and y, but only the last two modify the value of x.

-Lee

Edit: a few more for kicks:
++ is a unary operator. It increments its operand by 1. It evaluates to the new value of its operand if the operand appears on the right. It evaluates to the original value of its operand if the operand appears on the left.

x++
x is incremented by 1, evaluates to the original value of x.

++x is incremented by 1, evaluates to the new value of x.

chown33
Mar 21, 2013, 10:55 AM
int myresult = [recid.stringValue intValue]; <<<--- This what I am using to try and return recId from my Entity. This only returns 0.


Use the debugger to confirm the value of recid and the value of recid.stringValue. If either one is nil, then it's likely that myresult will be 0. Once you identify whether those values are nil, you can apply the debugger again to discover why the variables are nil.

If you don't know how to use the debugger, learn it now. It will be an extremely valuable tool for confirming what really happens, as compared to what you expect or believe is happening.

Debugging is the process of confirming your beliefs or expectations. If you have no way to do that, then you can't debug your program. When you wrote this:
What I need to do is retrieve RecId conents which contains a integer.
this is what you really meant:
What I need to do is retrieve RecId conents which I believe contains a integer.
or this:
What I need to do is retrieve RecId conents which I expect to contain a integer.
Debugging is applying tools and a debugging procedure, which allows you to say things like:
What I need to do is retrieve RecId conents which I have confirmed contains a integer using the debugger.

KieferThomas
Mar 21, 2013, 12:46 PM
Hey Everyone,

With all the information which was supplied I was able to look at things differently and found what I was doing wrong.

Chown33, thanks for the advice on the debugger. I in fact was getting the int value.

I got to looking things over closer and came up with: myresult = (int*)myresult + 1;

This has taken care of all my problems...

Thanks Again,
Kiefer

chown33
Mar 21, 2013, 01:59 PM
I got to looking things over closer and came up with: myresult = (int*)myresult + 1;


This code is incorrect. It may work, but that doesn't mean it's correct.

The type of myresult is int. Why would you type-cast it to the type "pointer to int", simply to add 1 to it?

I have a feeling you don't really understand the fundamentals of C or Objective-C.

If you don't know what it means when I wrote "The type of myresult is int", or you don't know the difference between int and pointer-to-int, then you need to review the fundamental types of C or Objective-C. Since the types in this case are identical for C and Objective-C, any tutorial or reference on C would work. You don't need a tutorial specifically on Objective-C to learn that.