iPhone - Sintetizzare e utilizzare ivar al di fuori della classe?
-
28-10-2019 - |
Domanda
Ho letto molte cose sulla chiamata @synthesize.Sul suo utilizzo, ...
Quindi ho fatto un test e il suo risultato mi dà una strana sensazione.
Mi spiego ...
Consente di scrivere in un oggetto .h
@interface AnObject : NSObject {
NSString* aaTest;
}
@property(nonatomic, retain) NSString* bbTest;
-(void)log;
Quindi nel suo .m
@synthesize bbTest = aaTest;
-(void)log {
NSLog(@"Inside var : %@", aaTest);
NSLog(@"Inside property : %@", self.bbTest);
}
In un altro .m, scriviamo:
#import "AnObject.h"
quindi in un unico metodo:
AnObject* testCtrl = [[AnObject alloc] init];
testCtrl.bbTest = @"Some string";
NSLog(@"Outside property : %@", testCtrl.bbTest);
[testCtrl log];
Siamo d'accordo che qui, includendo solo .h, la chiamata a synth non sia nota dall'altro oggetto.Guardando il registro, dà:
Outside property : Some string
Inside var : Some string
Inside property : Some string
Quindi ... non è strano?
Soluzione
Nella tua chiamata synthesize, assegni bbtest a aaTest (nota la T maiuscola).Non è la stessa cosa di aatest
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow