Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 21, 2013, 10:26 AM   #1
KieferThomas
macrumors newbie
 
Join Date: Mar 2012
Need To Get Value From Core Data textField Int and add + 1 to Integer

Hi All,
This is the code I have so far. What I need to do is retrieve RecId conents which contains a integer.

Code:
- (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

Last edited by dejo; Mar 21, 2013 at 10:54 AM. Reason: Please use [code] tags.
KieferThomas is offline   0 Reply With Quote
Old Mar 21, 2013, 10:40 AM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
+ 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.

Last edited by lee1210; Mar 21, 2013 at 10:51 AM.
lee1210 is offline   0 Reply With Quote
Old Mar 21, 2013, 10:55 AM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by KieferThomas View Post
Code:
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.
chown33 is offline   0 Reply With Quote
Old Mar 21, 2013, 12:46 PM   #4
KieferThomas
Thread Starter
macrumors newbie
 
Join Date: Mar 2012
Resolved

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
KieferThomas is offline   0 Reply With Quote
Old Mar 21, 2013, 01:59 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by KieferThomas View Post
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.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Having an issue passing data between views with Core Data. iJustinCabral iPhone/iPad Programming 0 Jul 15, 2013 09:50 PM
how do i make a integer (int) show up in a different view skater618 iPhone/iPad Programming 24 Aug 27, 2012 09:54 AM
Add or delete Core Data entries in an update? isthisonetaken iPhone/iPad Programming 1 Jul 30, 2012 11:24 AM
Saving data from custom tablecells core data luke3 iPhone/iPad Programming 0 Jul 14, 2012 06:09 PM
C++ Overloading - How do I call (int, ...) instead of (int, int, ...)? ArtOfWarfare Mac Programming 7 Jun 18, 2012 04:00 PM

Forum Jump

All times are GMT -5. The time now is 03:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC