2014-10-14 1 views
6

이전 Obj-c 앱을 기반으로하는 새로운 Swift 앱을 제작하고 있습니다. 내가했던파일 범위? 신속한 대리인 및 프로토콜

class MyAppViewController: CustomViewController { 

var delegate: MyAppViewControllerDelegate? 
protocol MyAppViewControllerDelegate{ 
func myAppViewController(controller: MyAppViewController, loggedInStudent:  MYStudent) 
func myAppViewControllerWantsSignUp(controller: MyAppViewController) 

: 저는 현재

여기

내 OBJ-C 코드는 내가 한 SWIFT에서 .H 파일에 같은

@interface MyAppViewController : CustomViewController 
@property (nonatomic, weak) id<MyAppViewControllerDelegate> delegate; 
@end 

@protocol MyAppViewControllerDelegate <NSObject> 
- (void)myAppViewController:(MyAppViewController *)controller loggedInStudent: (MYStudent *)student; 
- (void)myAppViewControllerWantsSignUp:(MyAppViewController *)controller; 
@end 

을 속입니다 대표 일하고 있어요 이것에 대한 독서와 연구가 많이 있었기 때문에 나는 기본적으로 올바르게하고 있다고 생각했다. (완전히 새로운 것이었지만 ...)

의 "Declaration is only valid in file scope" 이 클래스 내에서 선언과 관련이 있다고 가정 했으므로 클래스 내에서 내 코드가 선언 한 대리자 변수를 인식하지 못했습니다.

아이디어가 있으십니까?

답변

6

이 될해야 : 당신이 MyAppViewController를 소유하는 객체가 그 대표이다 일반적인 패턴에 따라하는 경우

protocol MyAppViewControllerDelegate { 
    func myAppViewController(controller: MyAppViewController, loggedInStudent:  MYStudent) 
    func myAppViewControllerWantsSignUp(controller: MyAppViewController) 
} 

class MyAppViewController: CustomViewController { 

    var delegate: MyAppViewControllerDelegate? 
} 

,이 메모리 문제가 발생할 수 있습니다,하지만. 당신은 약한 대표단과 같이 할 수 있도록 class 입력을 사용할 수 있습니다

protocol MyAppViewControllerDelegate : class { 
    func myAppViewController(controller: MyAppViewController, loggedInStudent:  MYStudent) 
    func myAppViewControllerWantsSignUp(controller: MyAppViewController) 
} 

class MyAppViewController: CustomViewController { 

    weak var delegate: MyAppViewControllerDelegate? 
} 

는 대리인을위한 클래스를 사용하는 당신이 필요하기 때문에이 다소 제한되어 있지만 도움을주기 :

을 유지 피할 수
2

귀하의 소스 코드에서 볼 수있는 것은 당신이 귀하의 프로토콜을 귀하의 클래스 안에 선언했다는 것입니다. 클래스 선언 외부에서 프로토콜을 선언하기 만하면됩니다.

업데이트 :

는 기본 액세스 레벨이 내부로 설정되어

내부 접근로 정의 자신의 정의 모듈에서 모든 소스 파일 내에서 사용되는 엔티티>가 아니라에서 할 수 있습니다 해당 모듈 외부의 모든 소스 파일. 일반적으로 응용 프로그램 또는 프레임 워크의 내부 구조를 정의 할 때 내부 액세스를 사용합니다.

Objective-C 또는 C와 달리 사용하기 전에 구현이 발생하지 않은 경우에는 앞으로 선언 할 필요가 없습니다.

자료 : The Swift Programming Language, Access Control