2016-09-01 2 views
0

Objective C 프로젝트가 있고 신속보기 컨트롤러에서 AppDelegate를 호출하려고합니다. 이 코드가 작동하지 않습니다.Swift Class에서 Objective C AppDelegate를 어떻게 참조합니까?

let appDelegate = 
     UIApplication.sharedApplication().delegate as! AppDelegate 

let managedContext = appDelegate.managedObjectContext 
+1

"작동하지 않는"정의 :

는 빠른 수업 시간에 당신은 그것을 호출 할 수 있어야합니다. 브리징 헤더를 추가 했습니까? – Paulw11

+0

AppDelegate에서 오류를 제거한 #import "AppDelegate.h"를 포함하는 가져 오는 헤더를 추가했습니다. 이제 오류가 발생했습니다 : let managedContext ... 인스턴스 멤버 'appDelegate'는 "ViewController"유형에서 사용할 수 없습니다 (클래스 이름은 ViewController.swift 임). 도움 감사. – John

답변

0

프로젝트에 머리말을 추가 했습니까?

위의 경우 브리징 헤더에 "AppDelegate.h"를 가져와야합니다.

guard let appDele = UIApplication.sharedApplication().delegate as? AppDelegate else { 
    fatalError("Unable to reference App Delegate, check settings and riding header") 
} 
let managedContext = appDele.managedObjectContext 
+0

"guard"원인 : 연속 줄의 선언은 ':'로 구분해야하며 예상되는 오류 ERROR MSGS.를 추가하면 문제가 해결되지 않습니다. 도움에 감사드립니다 – John