Le mie classi di modello sono principalmente implementate con metodi setter/getter sintetizzati e tutto andava bene. Tutto era ben collegato all'interfaccia utente. In seguito mi sono reso conto che la modifica di una proprietà avrebbe dovuto modificare altre proprietà (la modifica di type
potrebbe causare modifiche in minA
e maxA
), quindi ho scritto manualmente i metodi setter/getter per la proprietà type
. Codice segue:Perché ottengo un'eccezione non rilevata quando si implementano i miei metodi setter/getter KVC
QBElementType
@interface QBElementType : NSObject
@property NSRange minRange;
@property NSRange maxRange;
@end
@implementation QBElementType
@synthesize minRange;
@synthesize maxRange;
@end
QBElement
@interface QBElement : QBElementType{
QBElementType *_type;
}
@property (weak) QBElementType *type;
@property NSUInteger minA;
@property NSUInteger maxA;
@end
@implementation QBElement
@synthesize minA = _minA;
@synthesize maxA = _maxA;
- (QBElementType*)type
{
return _type;
}
- (void)setType:(QBElementType*)newType
{
[self willChangeValueForKey:@"type"];
_type = newType; // no need to bother with retain/release due to ARC.
[self didChangeValueForKey:@"type"];
/* Having the following 4 lines commented out or not changes nothing to the error
if (!NSLocationInRange(_minA, newType.minRange))
self.minA = newType.minRange.location;
if (!NSLocationInRange(_maxA, newType.maxRange))
self.maxA = newType.maxRange.location;
*/
}
@end
DOMANDA: Da allora, ogni volta che cambia il tipo di un elemento ottengo un un'eccezione non rilevata:
Impossibile aggiornare per osservatore < NSTableBinder ...> per il percorso della chiave "type.minRange" dal < QBElement ...>, molto probabilmente perché il valore per il "tipo" chiave è stata cambiata senza un adeguato KVO notifica inviata. Controllare la conformità KVO della classe QBElement.
C'è qualcosa di ovvio che mi manca da KVO-Compliance? Perché ottengo questo errore?
Ho provato a eseguire l'app senza 'willChangeValueForKey:' e 'didChange ....' ma l'interfaccia utente non ha mai ricevuto notifica della modifica! (Nota a margine: minA è una variabile a sé stante, potrebbe non apparire in questo modo perché ho semplificato il codice per questa domanda) –
In tal caso, c'è qualcos'altro che non va. Non dovresti davvero chiamare "willChange ..." e "doChange ..." in un setter del genere. Per quanto riguarda 'minA' essere una variabile a sé stante, capito, e in tal caso il mio suggerimento non funzionerà. Tuttavia, forse dovresti pubblicare il codice completo, nel caso in cui la tua semplificazione cambi o rimuova il problema. –