0

So im trying to figure out how to match two classes in objective-c from my Java code.

The java code is as follows:

import java.io.Serializable;

public interface IClient extends Serializable {
    String        getDeviceID();
    void          setDeviceID(String id);
    String        getNickname();
    void          setNickname(String name);
    boolean       matches(IClient client);
}

and the other class is as follows:

import xxx.xxx.xxx.interfaces.IClient;

public class Client implements IClient {
    private static final long serialVersionUID = 1L;

    private   String deviceid;
    private   String nickname;

    public Client() {
        deviceid  = "";
        nickname  = "";
    }

@Override
public String getDeviceID() {
    return this.deviceid;
}

@Override
public void setDeviceID(String id) {
    this.deviceid = (id == null) ? "" : id.trim();
}

@Override
public String getNickname() {
    return this.nickname;
}

@Override
public void setNickname(String name) {
    this.nickname = (name == null) ? "" : name.trim();
}

@Override
public boolean matches(IClient client) {
    if (client != null) {
        if (client == this) {
            return true;
        }
        if (client.getDeviceID().equals(deviceid) &&     client.getNickname().equals(nickname)) {
            return true;
        }
    }
    return false;
}
}

So I dont quite get how you would go about to convert the boolean, the code is on Objective-c atm is that IClient i a protocol and Client inherits it as follows:

Client.h

#import <Foundations/Foundation.h>
@protocol IClient <NSObject>
-(NSString*) DeviceID;
-(NSString*) Nickname;
-(BOOL) matches; // dont really know how to do that one
@end

IClient.h

#import <Foundations/Foundation.h>
#import "IClient.h"

@interface Client : NSObject <IClient>

//etc..

@end

Any help is welcome.

2 Answers2

0

Here is how is made a low level serialisation. The object level is with NSArchiver, NSCoder

Community
  • 1
  • 1
0

As Hot Licks already said in a comment, the correct parameter type for the match method is id<IClient>, which is "any Objective-C object that conforms to the IClient protocol", and you should use properties instead of instance variables.

Then you have:

IClient.h:

@protocol IClient <NSObject>
@property (nonatomic, copy) NSString *deviceID;
@property (nonatomic, copy) NSString *nickName;
-(BOOL)matches:(id<IClient>)client;
@end

Client.h:

#import <Foundation/Foundation.h>
#import "IClient.h"

@interface Client : NSObject <IClient>
// ...
@end

and finally Client.m:

#import "Client.h"

@implementation Client
@synthesize deviceID = _deviceID;
@synthesize nickName = _nickName;

- (BOOL)matches:(id<IClient>)client
{
    if (client != nil) {
        if (client == self)
            return YES;
        if ([client.deviceID isEqualTo:self.deviceID] && [client.nickName isEqualTo:self.nickName])
            return YES;
    }
    return NO;
}
@end

The @synthesize statements cause the compiler to create getter and setter methods, backed up by instance variables _deviceID, _nickName.

Normally one can omit the @synthesize statements as well, but properties defined in protocols are not synthesized automatically.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks that did the job, I have been using "@property" and "@synthesize" in other non protocol classes, was adviced to not use it in protcols but I guess it works. – Mikael Stenberg Sep 17 '13 at 16:12