Kevin Kevin - 11 days ago 10
iOS Question

Objective C incompatible

I'm new to Objective C, and am getting a little confused by the behavior of my code.

.m
file:

@interface MEDTNetworkAuthentication()

@property (nonatomic,retain) NSString *certificate;

@implementation MEDTNetworkAuthentication

-(void)setCertificate:(NSData*) certificateData
{
self.certificate=[self getCarelinkCertificateUsingData:certificateData];
return;
}

@end


.h
file:

#import <Foundation/Foundation.h>

@protocol NetworkAuthenticationDelegate;

@interface MEDTNetworkAuthentication : NSObject<NSURLSessionDelegate>

-(void)authenticate:(NSData*) certificateData andAuthData:(NSString*) authorization andURLSession:(NSURLSession*) careLinkAuthenticationSession;
-(void)setCertificate:(NSData*) certificateData;

@end


Now, the
authenticate
was existing functionality in the code before. Previously, I changed put
return;
after setting
self.certificate
, and it functions as expected. But since I want that previous functionality and don't want to get rid of it all, I wanted to create a new function (
setCertificate
). But when I add that function to the
.h
and
.m
files, now BOTH lines setting
self.certertificate
are giving me an error saying
Assigning to 'NSData *__strong' from incompatible type 'NSString *'
. When I delete the line for
setCertificate
from the
.h
file, the error goes away, but I can't access that function outside the m file now (obviously). What's going on?

Answer

Now that you've posted real code with real property names and real method names, the problem is obvious.

As I guessed, your property is named certificate, not cert. So your setCertificate: method is hiding the setter method for your certificate property.

Since the certificate property is of type NSString and your setCertificate: method takes a parameter of type NSData, you have a conflict.

The proper solution is to renamed your setCertificate: method so it doesn't conflict with your property.

Maybe change it to setCertificateData:.