좋아, 클래스를 하위 클래스로 지정하면 해당 클래스가 가진 모든 메서드와 속성을 상속하지만 여러 코드와 속성이 있고 두 개의 다른 클래스에 공통적으로 적용되기를 원한다고 가정하면 상속으로 가능합니까?
예를 들어 UIViewController와 UITableViewController가 있다고 가정합니다.
내가 당신의 질문을 이해한다면 대답은 아니오입니다. 동일한 클래스에서 원하는만큼의 클래스를 상속받을 수 있지만 한 클래스가 두 클래스에서 상속되도록 할 수는 없습니다. 당신에게 효과가있는 것은 싱글 톤 클래스를 만드는 것입니다. 화면의 중간 지점 찾기, 캐시에서 파일 제거, 파일 복사 등과 같은 일반적인 작업에 사용하는 유틸리티 클래스가 있습니다. 여러 클래스에서 사용하는 20 개 정도의 메서드가 있습니다. 시스템 버전, 글꼴, 텍스트 크기, 플레이어 이름 등과 같은 전역 변수를 설정하고 가져 오는 데 사용하는 또 다른 단일 클래스가 있습니다.
다음과 같은 유틸리티 메서드를 호출합니다.
NSString *resultsFilePath = [Utilities cachedFilePath:@"Results"];
NSString *fullResultsFilePath = [Utilities cachedFilePath:@"FullResults"];
NSString *troublesomeWordsFilePath = [Utilities cachedFilePath:@"TroublesomeTargets"];
[Utilities copyCachedResultsToFile];
그리고 다음과 같은 전역 변수 :
if ([Globals sharedInstance].currentClient) {
self.clientInput.text = [Globals sharedInstance].currentClient;
}
내 유틸리티 클래스는 다음과 같이 시작됩니다.
//
// Utilities.m
//
// Created by John Scarry on 11/3/11.
// Copyright (c) 2011 Learning Fundamentals, Inc. All rights reserved.
//
#import "Utilities.h"
#import "mach/mach.h"
@implementation Utilities
+ (CGPoint)findMidpoint:(UIView *)view {
CGPoint midPoint;
midPoint.x = view.bounds.origin.x + view.bounds.size.width/2;
midPoint.y = view.bounds.origin.y + view.bounds.size.height/2;
return midPoint;
}
+ (NSString *)formattedDate {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];
return todaysDate;
}
+ (NSString *)formattedClientName {
NSString *client = [NSString stringWithFormat:@" "];
if( [Globals sharedInstance].currentClient ) client = [NSString stringWithFormat:@" %@ ",[Globals sharedInstance].currentClient];
return client;
}
내 Globals 클래스는 다음과 같이 시작됩니다.
//
// Globals.m
//
// Created by John Scarry on 11/3/11.
// Copyright (c) 2011 Learning Fundamentals, Inc. All rights reserved.
//
#import "Globals.h"
@implementation Globals
static Globals *singleton = nil;
+(Globals *) sharedInstance {
if (nil != singleton) return singleton;
static dispatch_once_t onceToken; // lock
dispatch_once(&onceToken, ^{ // this code is called at most once
singleton = [[Globals alloc] init];
});
return singleton;
}
// Lots of the properties use a default value from the .pch file
// Use lazy instantiation to overide the getter to make sure it is set.
- (NSInteger) systemVersionNumber {
return [[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue];
}
- (NSString *)scoringType {
if ( !_scoringType ) _scoringType = SCORING_TYPE;
return _scoringType;
}
- (NSString *)targetSoundDelayCode {
if ( !_targetSoundDelayCode ) _targetSoundDelayCode = TARGET_SOUND_DELAY;
return _targetSoundDelayCode;
}
- (BOOL)checkBoxes {
if ( !_checkBoxes ) _checkBoxes = FORCED_CHOICE_SCORING;
return _checkBoxes;
}
- (BOOL)showFavorites {
if ( ! _showFavorites ) _showFavorites = NO;
return _showFavorites;
}
편집 : 많은 전역에 대한 기본값을 설정하는 각 앱에 .pch를 사용합니다. 예 :
#define FORCED_CHOICE_SCORING NO
#define SCORING_TYPE @"CDI"
그런 다음 사용자가 변경하지 않은 경우 기본값을 사용하는 사용자 지정 getter를 작성합니다.
- (NSUInteger)targetSoundDelay {
if ( !_targetSoundDelay ) _targetSoundDelay = TARGET_SOUND_DELAY];
return _targetSoundDelay;
}
- (NSString *)scoringType {
if ( !_scoringType ) _scoringType = SCORING_TYPE;
return _scoringType;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다