NSString 인스턴스 변수 "planetName"이 나 (아래 예제 에서처럼)에 의해 할당/해제 될 필요가 있는지 또는 클래스 인스턴스가 생성/할당 될 때 수행되는 것인가?인스턴스 변수 할당?
나의 이해는 INT와 플로트가 될 필요가 없습니다,하지만있는 NSString &있는 NSArray에 대해 확실하지 ...
이 같이@interface PlanetClass : NSObject {
NSString *planetName;
}
- (NSString *)planetName;
- (void)setPlanetName:(NSString *)value;
@end
...
- (id) init {
[super init];
planetName = [[NSString alloc] init];
return self;
}
- (void) dealloc {
[planetName release];
[super dealloc];
}
를 ** 않는다는 것입니다 - --------------------------------- ** EDIT : EDIT : 여기 다른 버전이 있습니다 ** --- ------------------------------- **
int main(int argc, const char *argv[]) {
// ** Allocated here
PlanetClass *newPlanet_01 = [[PlanetClass alloc] init];
NSString *newPlanetName = [NSString alloc] init];
// ** Set the instance variable pointer here
newPlanetName = @"Jupiter";
[newPlanet_01 setPlanetName:newPlanetName];
// ** Released here
[newPlanet_01 release];
return 0;
}
초기화 &의 dealloc 방법은 다음 ... 이렇게 될 것
- (id) init {
[super init];
return self;
}
- (void) dealloc {
// Always release the current copy of planetName
// pointed to by the class instance.
[planetName release]
[super dealloc];
}
setPlanetName 방법은 다음과 같을 것입니다 ...
- (void)setPlanetName:(NSString *)newPlanetName {
if (planetName != newPlanetName) {
[planetName release];
planetName = [newPlanetName copy];
}
}
PS : 나는 속성을 사용하거나 합성하고 있지 않다, 나는 아직까지 그것을 얻지 못했다.
환호
'NSString * newPlanetName = [NSString alloc] init]'은 즉시 다른 NSString을 변수에 할당하고 이전 값을 해제하지 않기 때문에 그냥 메모리 누수입니다. – Chuck