2012-07-12 34 views
2

我已經使用java的數據傳輸對象是一樣如何使用DTO的目標C

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

我如何可以使用相同的目標C.Probably「目標C的屬性」,會達到目的我認爲。但是,有人可以詳細解釋它,如何使用Objective C的屬性編寫DTO並有效地使用它們並保持內存安全(避免內存泄漏)。

如果可能的話,請嘗試上面的代碼轉換爲的OBJ C.

回答

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/

+0

不要忘了[用戶名發佈]; ......在的dealloc(如果不是ARC) – samir 2012-07-12 09:52:40

+0

@Inder Kumar Rathore Getter和Setter在哪?..? – 2012-07-12 10:04:23

+0

@property用於setter和getters。 'accountDEMObj.username = @「abc:;'是setter的語法..用於進一步閱讀鏈接。 – 2012-07-12 15:33:47

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中繼承)

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