ios - today widget NSTimer memory read error?
PHP中文网
PHP中文网 2017-04-18 09:23:04
0
1
620

在today widget里使用NSTimer,发生错误警报:error: memory read failed for 0x0

class TodayViewController: UIViewController, NCWidgetProviding {
    var timer: NSTimer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view from its nib.
        print("widget")
        
        self.preferredContentSize = CGSizeMake(0,100);

        timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector(getBattery()), userInfo: nil, repeats: true)
    }
    
    func getBattery() {
        print("level")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
        // Perform any setup necessary in order to update the view.

        // If an error is encountered, use NCUpdateResult.Failed
        // If there's no update required, use NCUpdateResult.NoData
        // If there's an update, use NCUpdateResult.NewData
        
        completionHandler(NCUpdateResult.NewData)
    }
    
    deinit {
        timer.invalidate()
    }
}
PHP中文网
PHP中文网

认证高级PHP讲师

모든 응답(1)
小葫芦

추측해 보겠습니다.
1. 오류: 0x0에 대한 메모리 읽기에 실패했습니다. 0x0은 해제된 객체입니다. 객체가 해제된 후 호출되므로 오류가 발생합니다.
2. 으아악

Deinit는 dealloc과 동일합니다. 자체 순환 참조가 해제되기 전에 먼저 타이머를 제거한 다음 deinit를 호출해야 합니다. 여기에 뭔가 문제가 있는 것 같아요. 적절한 시간에 타이머를 일시 중지한 다음 타이머를 nil로 설정하세요.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!