Вопрос по objective-c – Если вы установите для делегата значение nil в классе, используя делегат или в самом классе

13

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

// somewhere in class A

- (void) someFunc {
  self.b = [[B alloc] init];
  self.b.delegate = self;
}

- (void) dealloc {
  self.b.delegate = nil;
  [self.b release];
}
self.b = [[B alloc] init];Error: User Rate Limit ExceededselfError: User Rate Limit ExceededallocError: User Rate Limit Exceeded@propertyError: User Rate Limit ExceededbError: User Rate Limit Exceeded thesummersign
Error: User Rate Limit Exceeded thesummersign
Error: User Rate Limit Exceeded Brad Thomas

Ваш Ответ

3   ответа
15

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
5

Error: User Rate Limit Exceeded

  1. You've forgotten to call [super dealloc] at the end of your own dealloc method.
  2. Since 'a' created 'b', and if no other objects have retained 'b', there no point in nilling the delegate in the -dealloc, since 'b' is about to be destroyed anyhow. If it's possible that other objects have a reference to 'b' (meaning it might outlive 'a') then set the delegate to nil.
  3. Object 'b' should be the one to take care of its delegate in its own -dealloc if necessary. (Generally, the delegator does not retain the delegate.)
  4. Avoid using properties in -init... and -dealloc methods — Apple discourages this, and for good reason. (Not only could it have unexpected side effects, but can also cause nastier, crashier problems.)
  5. Using properties (via the dot syntax) when you don't need to invisibly adds extra work. For instance, self.b.delegate = self is equivalent to [[self getB] setDelegate:self] — it's just syntactic sugar that makes it look like you're accessing the ivar directly, but you're actually not.
  6. Using properties without understanding what they do can lead to trouble. If self.b retains the value (the property is set to "assign"), you have a memory leak on your hands.

Error: User Rate Limit Exceeded

- (void) someFunc {
  b = [[B alloc] init];
  b.delegate = self; // or [b setDelegate:self];
}

- (void) dealloc {
  b.delegate = nil;
  [b release];
  [super dealloc];
}
8

Error: User Rate Limit Exceeded

@property (assign) id<BDelegate> delegate;

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Boon
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы