Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Property attribute syntax for passing NSNumber object

I am working on a project that takes UITextField inputs and passes them to a managedObjectContext for core data.

I have a date field, several text fields and one numerical field. I can pass the date and text fields but don't know the property attribute I need to pass the number (its a float).

Here is my saveData method - where I intercept the text field data and pass it to Core Data:

- (IBAction)saveData:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    //create a new managed object

    NSManagedObject *newFlight = [NSEntityDescription insertNewObjectForEntityForName:@"FlightEvent" inManagedObjectContext:context];
    [newFlight setValue:self.flightDate.date forKey:@"flightDate"];
    [newFlight setValue:self.departureAirport.text forKey:@"departureAirport"];
    [newFlight setValue:self.arrivalAirport.text forKey:@"arrivalAirport"];
    [newFlight setValue:self.aircraftModel.text forKey:@"aircraftModel"];
    [newFlight setValue:self.totalFlightTime forKey:@"totalFlightTime"];

So statements 1 - 4 (lines truncated for clarity):

self.flightDate.date forKey:@"flightDate"
self.departureAirport.text forKey:@"departureAirport"
self.arrivalAirport.text forKey:@"arrivalAirport"
self.aircraftModel.text forKey:@"aircraftModel"

Work just fine but the last statement:

self.totalFlightTime(need .something here!) forKey:@"totalFlightTime"

I can't figure out the correct attribute to use. In my header file the @property is declared as an NSNumber.

Can anyone give me some sage advice? Thanks!

8 Answers

looking at the error you are getting the property you are passing to setValue is apparently a UITextfield, not an NSNumber. what you need to do is convert the string from your text field to an NSNumber using an NSNumberFormatter

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *totalFlightTimeNumber = [formatter numberFromString:self.totalFlightTime.text]; 

then use the totalFlightTimeNumber variable instead of your self.totalFlightTime

[newFlight setValue:totalFlightTimeNumber forKey:@"totalFlightTime"];

setValue should work. What errors are you getting when you try to use setValue?

I think I need to add property attribute on the

self.totalFlightTime forKey:@"totalFlightTime"

so that it's

self.totalFlightTime.SOMETHING forKey:

The code compiles but crashes when I attempt to save and segue away from the view controller. The error is:

'Unacceptable type of value for attribute: property = "totalFlightTime"; desired type = NSNumber; given type = UITextField;

my header file is:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "FlightEvent.h"

@interface FPLManualEntryViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIDatePicker *flightDate;
@property (weak, nonatomic) IBOutlet UITextField *departureAirport;
@property (weak, nonatomic) IBOutlet UITextField *arrivalAirport;
@property (weak, nonatomic) IBOutlet UITextField *aircraftModel;
@property (weak, nonatomic) IBOutlet UITextField *totalFlightTime;

- (IBAction)saveData:(id)sender;


@end

In core data my FlightEvent NSManagedObject subclass header file is:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface FlightEvent : NSManagedObject

@property (nonatomic, retain) NSString * aircraftModel;
@property (nonatomic, retain) NSString * arrivalAirport;
@property (nonatomic, retain) NSString * departureAirport;
@property (nonatomic, retain) NSDate * flightDate;
@property (nonatomic, retain) NSDate * timeStamp;
@property (nonatomic, retain) NSNumber * totalFlightTime;

@end

id rename your properties to reflect if its a text field or not. you have a lot of properties with the same name which can get confusing. Id change your textfield properties to have textField on the end of the property name like totalFlightTimeTextField etc.

Sure, I get that. But it seems I'm missing the .text syntactic equivalent for an NSNumber. Is there such an attribute, or am I mission something?

Success! Thanks a ton. I was attempting the same thing you described using floatValue but your method is much more elegant, and it's much easier to deal w/ NSNumbers in Core Data rather than scalar types. Thanks!! Compiles and runs w/ no problem. Off to the next thing.

BTW I refactored the properties as you recommended and the code is indeed much more readable.

no problem. sorry it took me a while to see what you were attempting to do since I havent worked with core data much, but eventually it clicked haha. simple fix

No sweat. I take solace in finding small solutions every day. Thanks for taking the time.