내 응용 프로그램의 모든 클래스간에이 토큰 (또는 기록 된 APIClient의 인스턴스)을 공유하는 방법은 무엇입니까?
- 만들기입니다 이는
shared instance
-
- 파괴 네트워크 요청
- 에서 성공 토큰의 수신에 초기화가 보유하고있는 특성 (성공 토큰)이 더 이상 필요하지 않은 경우
일부 샘플 코드는이를 달성하기 위해 :
// APIHelper.h
@interface APIHelper : NSObject
@property (nonatomic) NSString *mySuccessToken; // can be any data type
+ (instancetype)sharedInstance;
@end
// APIHelper.m
@implementation APIHelper
+ (instancetype)sharedInstance{
static dispatch_once_t once;
static APIHelper *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [self new];
});
return sharedInstance;
}
@end
// Usage of the APIHelper shared instance
// In the function responsible for firing the network request
[MyFetchRequestWithSuccess:^{
...
[APIHelper sharedInstance].mySuccessToken = receivedSuccessToken; // update the shared instance with your received success token from the request
} failure:^{
...
}]
당신은 클래스와 정적 변수를 생성하고 바로 클래스 이름과 런타임에 알려진 것입니다 내가 토큰으로 정적 변수를 만들 수있는 방법 변수 –
를 호출 할 수 있습니다 만 ? – zzheads
토큰을 무엇을 의미합니까? API 호출에서 전달해야하는 문자열입니다. –