I am trying to implement a simple factory method for creating a new entity for my core data database. In Pet+Create.h
:
+ (Pet *)petWithName:(NSString *)name
weight:(NSNumber *)weight
weightIsInKg:(BOOL)yesorno
inManagedObjectContext:(NSManagedObjectContext *)context
In Pet+Create.m
:
+ (Pet *)petWithName:(NSString *)name
weight:(NSNumber *)weight
weightIsInKg:(BOOL)yesorno
inManagedObjectContext:(NSManagedObjectContext *)context
{
Pet *newPet = nil;
newPet = [NSEntityDescription insertNewObjectForEntityForName:@"Pet" inManagedObjectContext:context];
newPet.name = name;
newPet.idealWeight = weight;
newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
return newPet;
}
And finally, in Pet.h
: (which is a generated file by xcode, I have not touched it)
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Pet : NSManagedObject
@property (nonatomic, retain) NSNumber * idealWeight;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * weightIsInKg;
@end
My problem is, I am getting a Use of undeclared identifier: weightIsInKg
error message on newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
Why am I getting this error? I have obviously declared weightIsInKg
because it is in the method name! Am I missing something simple?
The parameter with which petWithName
is called is named yesorno
weightIsInKg:(BOOL)yesorno
then you refer to it as weightIsInKg
newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
Use instead
newPet.weightIsInKg = [NSNumber numberWithBool:yesorno];
Your method argument is called yesorno
, not weightIsInKg
. Use yesorno
or sanitize your variable names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.