Reputation: 3647
I have been using Data Transfer Objects in Jave, like
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;
}
...
}
How can i use the same in Objective C.Probably "Properties of Objective C" will serve the purpose i think. But can some one explain it in detail, how to use the property of Objective C to write DTO's and using them effectively and to keep it memory safe(avoiding memory leaks).
If possible , please try converting the above code to Obj C.
Upvotes: 2
Views: 1893
Reputation: 3682
I think that DTO pattern should contain object read/write from-to external format like dictionary in my case
// .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
Upvotes: 0
Reputation: 665
Modern answer:
@interface AccountDEM : NSObject
@property (nonatomic) NSString *userName;
@property (nonatomic) NSString *userAuthToken;
@property (nonatomic) NSNumber *user_Id;
@end
@implementaion
// no need in additional code
@end
Don't forget to subclass from NSObject (most cases; you can also subclass from NSProxy)
Upvotes: 1
Reputation: 39978
@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
For properties you can read http://objective-c-properties.blogspot.in/
Upvotes: 2