你如何匹配objective-c中的两个类

时间:2013-09-17 11:59:08

标签: java objective-c

所以我试图弄清楚如何在Objective-c中匹配我的Java代码中的两个类。

java代码如下:

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);
}

,另一个类如下:

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;
}
}

所以我不太明白你将如何转换布尔值,代码在Objective-c atm上是IClient我是一个协议,Client继承如下:

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

欢迎任何帮助。

2 个答案:

答案 0 :(得分:0)

Here是如何进行低级序列化的。对象级别为NSArchiverNSCoder

答案 1 :(得分:0)

正如Hot Licks已经在评论中说的那样,匹配方法的参数类型正确 是id<IClient>,它是“符合IClient协议的任何Objective-C对象”, 你应该使用属性而不是实例变量。

然后你有:

<强> 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

最后 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

@synthesize语句使编译器创建getter和setter方法, 由实例变量_deviceID_nickName备份。

通常也可以省略@synthesize语句,但定义了属性 在协议中自动合成。