OOP,目标 c 中的聚合

发布于 2024-10-11 22:12:50 字数 840 浏览 4 评论 0原文

我有一些复杂的对象,我不想实现它并且稍后会出现内存泄漏,所以必须询问:) 这是进行聚合的好方法吗?我是否需要以及在哪里从用户中清除角色对象?

#import "Role.h"

@interface User : NSObject {
    NSString *firstName;
    NSString *lastName;

    Role *role;
}
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;

@property (nonatomic, retain) Role *role;
@end
#import "User.h"

@implementation User
@synthesize firstName;
@synthesize lastName;

@synthesize role;
@end

@interface Role : NSObject {
    NSInteger *roleId;
    NSString *title;
    NSString *description;
}
@property (nonatomic, retain) NSInteger *roleId;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *description;
@end
#import "Role.h"

@implementation Role
@synthesize roleId;
@synthesize title;
@synthesize description;
@end

I have some complex objects and I don't want to implement it and have memory leaks later, so must ask :)
Is this good way of doing aggregation? Do I need and where to clean Role object from User?

#import "Role.h"

@interface User : NSObject {
    NSString *firstName;
    NSString *lastName;

    Role *role;
}
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;

@property (nonatomic, retain) Role *role;
@end
#import "User.h"

@implementation User
@synthesize firstName;
@synthesize lastName;

@synthesize role;
@end

@interface Role : NSObject {
    NSInteger *roleId;
    NSString *title;
    NSString *description;
}
@property (nonatomic, retain) NSInteger *roleId;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *description;
@end
#import "Role.h"

@implementation Role
@synthesize roleId;
@synthesize title;
@synthesize description;
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2024-10-18 22:12:50

您的 dealloc 方法需要如下所示:

对于用户:

-dealloc
{
    [firstName release];
    [lastName release];
    [role release];
    [super dealloc];
}

对于角色:

-dealloc
{
    [title release];
    [description release];
    [super dealloc];
}

roleId 不需要释放,因为 NSInteger 实际上是原始整数类型的 typedef。这也意味着你的属性声明是错误的,它应该是:

@property (nonatomic, assign) NSInteger roleId;

另外,NSString 是不可变的,它实现了复制协议,所以你的 NSString 属性应该是 copy 而不是 retain。例如

@property (nonatomic, copy) NSString *firstName;

编辑

正如 Björn 指出的,Role 的界面应该是:

@interface Role : NSObject {
    NSInteger roleId;  // not a pointer
    NSString *title;
    NSString *description;
}
@property (nonatomic, assign) NSInteger roleId;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *description;
@end

Your dealloc methods need to look like the following:

For user:

-dealloc
{
    [firstName release];
    [lastName release];
    [role release];
    [super dealloc];
}

For Role:

-dealloc
{
    [title release];
    [description release];
    [super dealloc];
}

roleId does not need to be released because NSInteger is actually a typedef to a primitive integer type. That also means your property declaration is wrong, it should be:

@property (nonatomic, assign) NSInteger roleId;

Also, NSString is immutable and it implements the copying protocol so your NSString properties should be copy not retain. e.g.

@property (nonatomic, copy) NSString *firstName;

Edit

As Björn points out, the interface for Role should be:

@interface Role : NSObject {
    NSInteger roleId;  // not a pointer
    NSString *title;
    NSString *description;
}
@property (nonatomic, assign) NSInteger roleId;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *description;
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文