Reputation: 321
I'm writing a client that hits an API. The API spits out this:
Id: 33,
UserUrl: "https://example.com/username/profile",
Title: "I wanna give 'em the holy boot!",
IsOnline: "no"
I consume the JSON as a NSDictionary and convert it to my User object using setValuesForKeysWithDictionary:. My User.m has custom setters to take the incoming UserUrl string and bend it into a NSURL like so:
- (void)setUserUrl:(NSString *)stringInput {
_UserUrl = [NSURL URLWithString:stringInput];
}
This works fine. When I try the same with the BOOLs, I get a warning:
Conflicting parameter types in implementation of 'setIsSecret:': 'BOOL' (aka 'signed char') vs 'NSString *__strong'
- (void)setIsOnline:(NSString *)stringInput {
if ([stringInput isEqualToString:@"yes"])
_IsOnline = YES;
else
_IsOnline = NO;
}
User.h
@interface Achievement : NSObject
@property (strong, nonatomic) NSNumber *Id;
@property (strong, nonatomic) NSURL *UserUrl;
@property (strong, nonatomic) NSString *Title;
@property (nonatomic) BOOL IsOnline;
@end
My code compiles and runs fine, I'd just like to know why it warns me on the BOOL property but not the NSString property. This is for an API wrapper, so I'd prefer not to surpress the warnings. Thanks in advance.
UPDATE rmaddy had the correct answer. For anyone looking to do the same thing in the future, create a custom initializer to do all your type conversions.
Upvotes: 1
Views: 704
Reputation: 318934
You declare the IsOnline
property to have a type of BOOL
but your setIsOnline:
method takes an NSString *
parameter. It needs to be a BOOL
parameter.
Remember, the IsOnline
property results in the synthesize methods:
- (BOOL)IsOnline;
- (void)setIsOnline:(BOOL)value;
Unlike other languages, Objective-C doesn't support method overloading. The data type of the parameter is not part of the method name. Your:
- (void)setIsOnline:(NSString *)value
method conflicts with the expect one.
You should be getting the same error for your setUserUrl:
method too since the property type is NSURL *
but your method takes an NSString *
. I'm not sure why you don't have an issue with that one.
If you want a separate method for setting a property given a different type then you should have a method name that is different than the standard property setter method name.
Side note - it is standard practice that variable, property, and method names begin with lowercase letters, not uppercase. Only classes should start with uppercase letters.
Upvotes: 1