0

所以我试图从我的Java代码中找出如何匹配objective-c中的两个类。

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 ia 协议和 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

欢迎任何帮助。

4

2 回答 2

0

正如 Hot Licks 在评论中已经说过的,match 方法的正确参数类型是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

客户端.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语句,但协议中定义的属性不会自动合成。

于 2013-09-17T13:01:58.640 回答
0

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

于 2013-09-17T12:06:03.723 回答