2

我一直在 Jave 中使用数据传输对象,例如

public class AccountDEM
{
  private String userName;
  private String userAuthToken;
  private int user_Id;

  public void setuserName(String _userName)
  {
    this.userName=_userName;
  }
  public string getuserName()
  {
    return userName;
  }
 ...
}

我如何在Objective C中使用相同的东西。可能“Objective C的属性”将达到我认为的目的。但是有人可以详细解释一下,如何使用Objective C的属性来编写DTO并有效地使用它们并保持内存安全(避免内存泄漏)。

如果可能,请尝试将上述代码转换为 Obj C。

4

3 回答 3

2
@interface AccountDEM
@property (nonatomic,retain) NSString* userName;
@property (nonatomic,retain) NSString* userAuthToken;
@property (nonatomic) int user_Id;
@end


@implementaion
@synthesize userName;
@synthesize userAuthToken;
@synthesize user_Id;
@end

对于属性,您可以阅读http://objective-c-properties.blogspot.in/

于 2012-07-12T09:15:10.277 回答
1

现代答案:

@interface AccountDEM : NSObject

@property (nonatomic) NSString *userName;
@property (nonatomic) NSString *userAuthToken;
@property (nonatomic) NSNumber *user_Id;

@end

@implementaion

// no need in additional code

@end

不要忘记从 NSObject 子类化(大多数情况下;您也可以从 NSProxy 子类化)

于 2014-06-04T11:18:28.277 回答
0

我认为在我的情况下,DTO 模式应该包含对象读取/写入外部格式,如字典

// .h
@interface AccountDEM

@property (nonatomic, copy) NSString* userName;
@property (nonatomic, copy) NSString* userAuthToken;
@property (nonatomic) int userIdentifier;

// transfer between dictionary and object
@property (nonatomic, readonly) NSDictionary *serialized;

- (instancetype) initWithDictionary:(NSDictionary *)serialized;

@end

// .m
@interface AccountDEM

@property (nonatomic, strong) NSDictionary *dictionaryRepresentation; 

@end

@implementaion

- (void)setObject:(id)object forKey:(id<NSCopying>)key {
    if (key) {
        if (obj) {
            NSMutableDictionary *dictionary = [self.dictionaryRepresentation mutableCopy];
            [dictionary removeObjectForKey:key];
            self.dictionaryRepresentation = [dictionary copy];
        }
        else {
            NSMutableDictionary *dictionary = [self.dictionaryRepresentation mutableCopy];
            dictionary[key] = obj;
            self.dictionaryRepresentation = [dictionary copy];
        }
    }
}

- (id)objectForKey:(id<NSCopying>)key {
    if (!key) {
        return nil;
    }

    return self.dictionaryRepresentation[key];
}

#pragma mark - Custom Getters/Setters
- (void)setUserName:(NSString *)userName {  
    [self setObject:userName forKey:@"UserName"];
}

- (NSString *)userName {
    return
    [self objectForKey:@"UserName"];
}

- (void)setUserAuthToken:(NSString *)userAuthToken {    
    [self setObject:userAuthToken forKey:@"UserAuthToken"];
}

- (NSString *)userAuthToken {
    return  
    [self objectForKey:@"UserAuthToken"];
}

- (void)setUserIdentifier:(int)userIdentifier {
    [self setObject:@(userIdentifier) forKey:@"UserIdentifier"];
}

- (int)userIdentifier {
    return  
    [[self objectForKey:@"UserIdentifier"] intValue];
}


#pragma mark - Transfer
- (NSDictionary *)serialized {
    return self.dictionaryRepresentation;
}

- (instancetype) initWithDictionary:(NSDictionary *)serialized {
    self = [super init];

    if (self) {
        // any validation about serialized object
        // and put it into dictionaryRepresentation
        _dictionaryRepresentation = serialized;
    }
    return self;
}

@end
于 2015-08-13T15:09:03.607 回答