2017-09-11 4 views
-1

NSDocument 하위 클래스가 처리하는 데이터를 구성하는 간단한 클래스가 있습니다. 하지만, NSDocument의 데이터 (ofType :)를 사용하여 클래스를 저장/초기화하려고 시도하고 (of : ofType :) 메소드를 읽으려고 할 때 문 밖으로 나왔습니다.NSCoder "추상 클래스 정의"문제가 발생했습니다.

내가 디버거에보고하고 오류는 다음과 같습니다

[일반] *** -encodeObject : forKey : 단지 추상 클래스에 대해 정의. - [NSArchiver encodeObject : forKey :]를 정의하십시오!

NSDocument의 내 문서 하위 클래스 보이는 같은 :

class Document: NSDocument 
{ 
    private(set) var coverTree = CoverTree() 

    // ... skipping boilerplate init and makeWindowControllers methods 

    override class func autosavesInPlace() -> Bool { 
    return false 
    } 

    override func data(ofType typeName: String) throws -> Data 
    { 
    return NSArchiver.archivedData(withRootObject: coverTree) 
    } 

    override func read(from data: Data, ofType typeName: String) throws 
    {   
    if let ct = NSUnarchiver(forReadingWith: data)?.decodeObject() as? CoverTree 
    { 
     coverTree = ct 
    } 
    } 
} 

내 CoverTree 클래스는 현재 다음과 같습니다 CoverTree 유일한 선을 실행할 때 오류가 발생합니다

import Cocoa 

class CoverTree: NSObject, NSCoding 
{ 
    private(set) var generated = false 

    let creationStamp : String 

    override init() 
    { 
    let df = DateFormatter() 
    df.dateStyle = .full 
    df.timeStyle = .full 

    creationStamp = df.string(from: Date()) 

    super.init() 
    } 

    required init(coder decoder:NSCoder) 
    { 
    creationStamp = decoder.decodeObject(forKey: "creation") as! String 
    } 

    func encode(with coder: NSCoder) 
    { 
    coder.encode(self.creationStamp, forKey:"creation") 
    } 

    func generate(dataSet : DataSet) -> Void 
    { 
    generated = true 
    } 
} 

:: 인코딩 ()

저는 StackOverflow를 사용하여 예제를 찾고 있습니다. 내가 가진 모든 제안 된 패턴/스 니펫을 따르는 것으로 보입니다.

오류 문자열을 검색하면 오류가 NSKeyedArchiver ...가 아닌 NSCoder를 사용하기 때문에 발생합니다.하지만 인코딩 할 문자의 부호를 (코드 작성기 NSKeyedArchiver와 함께) 변경하면 CoverTree가 아니라는 오류가 발생합니다. NSCoding을 준수합니다.

무엇이 누락 되었습니까?

2017-09-11 09:55:44.682537-0400 coverTreeDemo[40650:3798144] [General] *** -encodeObject:forKey: only defined for abstract class. Define -[NSArchiver encodeObject:forKey:]! 
2017-09-11 09:55:44.691417-0400 coverTreeDemo[40650:3798144] [General] (
    0 CoreFoundation      0x00007fffa3ae357b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x00007fffb8d3c1da objc_exception_throw + 48 
    2 CoreFoundation      0x00007fffa3b60c55 +[NSException raise:format:] + 197 
    3 Foundation       0x00007fffa5621abe NSRequestConcreteImplementation + 229 
    4 coverTreeDemo      0x0000000100003583 _TFC13coverTreeDemo9CoverTree6encodefT4withCSo7NSCoder_T_ + 163 
    5 coverTreeDemo      0x00000001000035fa _TToFC13coverTreeDemo9CoverTree6encodefT4withCSo7NSCoder_T_ + 58 
    6 Foundation       0x00007fffa54922e1 -[NSArchiver encodeRootObject:] + 164 
    7 Foundation       0x00007fffa5491a66 +[NSArchiver archivedDataWithRootObject:] + 141 
    8 coverTreeDemo      0x0000000100003f3b _TFC13coverTreeDemo8Document4datafzT6ofTypeSS_V10Foundation4Data + 491 
    9 coverTreeDemo      0x000000010000402b _TToFC13coverTreeDemo8Document4datafzT6ofTypeSS_V10Foundation4Data + 91 
    10 AppKit        0x00007fffa190121c -[NSDocument writeToURL:ofType:error:] + 812 
    11 AppKit        0x00007fffa1900ead -[NSDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 445 
    12 AppKit        0x00007fffa208bbb1 -[NSDocument(NSDocumentSaving) _writeSafelyToURL:ofType:forSaveOperation:forceTemporaryDirectory:error:] + 915 
    13 AppKit        0x00007fffa208c8b7 -[NSDocument(NSDocumentSaving) _writeSafelyToURL:ofType:forSaveOperation:error:] + 28 
    14 AppKit        0x00007fffa190089e -[NSDocument writeSafelyToURL:ofType:forSaveOperation:error:] + 355 
    15 AppKit        0x00007fffa20978c5 __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_2.1116 + 233 
    16 AppKit        0x00007fffa20977cd __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke.1113 + 454 
    17 AppKit        0x00007fffa2095903 __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_2.969 + 2071 
    18 Foundation       0x00007fffa56e8e48 __85-[NSFileCoordinator(NSPrivate) _coordinateWritingItemAtURL:options:error:byAccessor:]_block_invoke.404 + 226 
    19 Foundation       0x00007fffa56e88ab -[NSFileCoordinator(NSPrivate) _withAccessArbiter:invokeAccessor:orDont:andRelinquishAccessClaim:] + 493 
    20 Foundation       0x00007fffa557e206 -[NSFileCoordinator(NSPrivate) _coordinateWritingItemAtURL:options:error:byAccessor:] + 862 
    21 AppKit        0x00007fffa2092dd0 -[NSDocument(NSDocumentSaving) _fileCoordinator:coordinateReadingContentsAndWritingItemAtURL:byAccessor:] + 387 
    22 AppKit        0x00007fffa20950d4 __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke.964 + 544 
    23 AppKit        0x00007fffa209462b __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke + 508 
    24 AppKit        0x00007fffa1b2abd6 -[NSDocument(NSDocumentSerializationAPIs) continueFileAccessUsingBlock:] + 222 
    25 AppKit        0x00007fffa1b2b477 -[NSDocument(NSDocumentSerializationAPIs) _performFileAccess:] + 782 
    26 AppKit        0x00007fffa2094429 -[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:] + 92 
    27 AppKit        0x00007fffa194b2a8 __85-[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke_2 + 256 
    28 AppKit        0x00007fffa18ba1e2 -[NSDocument _commitEditingThenContinue:] + 474 
    29 AppKit        0x00007fffa18b9fff -[NSDocument _commitEditingWithDelegate:didSomethingSelector:contextInfo:thenContinue:] + 92 
    30 AppKit        0x00007fffa194b196 __85-[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke + 444 
    31 AppKit        0x00007fffa1b2a305 -[NSDocument(NSDocumentSerializationAPIs) performActivityWithSynchronousWaiting:usingBlock:cancellationHandler:] + 475 
    32 AppKit        0x00007fffa194afd4 -[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] + 113 
    33 AppKit        0x00007fffa2090ff1 __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke.595 + 113 
    34 AppKit        0x00007fffa1b2a4d3 -[NSDocument(NSDocumentSerializationAPIs) _continueActivityUsingBlock:] + 320 
    35 AppKit        0x00007fffa2090de5 __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke_2.580 + 506 
    36 AppKit        0x00007fffa209097c __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke.561 + 1500 
    37 AppKit        0x00007fffa1c558b9 -[NSSavePanel _didEndSheet:returnCode:contextInfo:] + 95 
    38 AppKit        0x00007fffa17c3b84 -[NSWindow _endWindowBlockingModalSession:returnCode:] + 308 
    39 AppKit        0x00007fffa1c581c6 -[NSSavePanel overwriteAlertDidEnd:returnCode:contextInfo:] + 256 
    40 AppKit        0x00007fffa197a1e3 -[NSAlert didEndAlert:returnCode:contextInfo:] + 90 
    41 AppKit        0x00007fffa17c3b84 -[NSWindow _endWindowBlockingModalSession:returnCode:] + 308 
    42 AppKit        0x00007fffa18239e3 -[NSAlert buttonPressed:] + 107 
    43 libsystem_trace.dylib    0x00007fffb984f3a7 _os_activity_initiate_impl + 53 
    44 AppKit        0x00007fffa1cd0721 -[NSApplication(NSResponder) sendAction:to:from:] + 456 
    45 AppKit        0x00007fffa17b4cc4 -[NSControl sendAction:to:] + 86 
    46 AppKit        0x00007fffa17b4bec __26-[NSCell _sendActionFrom:]_block_invoke + 136 
    47 libsystem_trace.dylib    0x00007fffb984f3a7 _os_activity_initiate_impl + 53 
    48 AppKit        0x00007fffa17b4b44 -[NSCell _sendActionFrom:] + 128 
    49 AppKit        0x00007fffa17f7539 -[NSButtonCell _sendActionFrom:] + 98 
    50 libsystem_trace.dylib    0x00007fffb984f3a7 _os_activity_initiate_impl + 53 
    51 AppKit        0x00007fffa17b3426 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481 
    52 AppKit        0x00007fffa17f7272 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798 
    53 AppKit        0x00007fffa17b1ddb -[NSControl mouseDown:] + 832 
    54 AppKit        0x00007fffa1e4c24f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341 
    55 AppKit        0x00007fffa1e48a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942 
    56 AppKit        0x00007fffa1e47f0a -[NSWindow(NSEventRouting) sendEvent:] + 541 
    57 AppKit        0x00007fffa1ccc681 -[NSApplication(NSEvent) sendEvent:] + 1145 
    58 AppKit        0x00007fffa1547427 -[NSApplication run] + 1002 
    59 AppKit        0x00007fffa1511e0e NSApplicationMain + 1237 
    60 coverTreeDemo      0x0000000100002cbd main + 13 
    61 libdyld.dylib      0x00007fffb961d235 start + 1 
) 
+0

중복에 의해 대체되었습니다. co.kr/questions/3892578/nscoding-protocol-questions? –

+0

그것이 내가 본 많은 질문/답변 중 하나이지만 문제를 해결하는 데 도움이되지 못했습니다. 핵심 문제가 압축기의 키가없는 버전을 사용하고 있었으므로이 정보가 3892578에 있음을 알 수 있습니다 ... 그러나 문제가 실제로 무엇인지 전혀 알지 못하면 해결책이 분명하지 않았습니다. – MikeMayer67

답변

1

문제는 당신이 NSArchiver 및 NSUnarchiver로 NSDocument 서브 클래스를 참조한다는 것입니다 :이 어떤 도움 인 경우

덕분에 많이 마이크

여기에 전체 스택 추적입니다. 그건 틀렸어요. 이러한 클래스는 더 이상 사용되지 않습니다. 그들을 대체해야 NSKeyedArchiver 및 NSKeyedUnarchiver 사용해야합니다. docs 이것에 대해 매우 명확 : // 유래 : 맥 OS 10.2 이상 NSArchiver 및 NSUnarchiver 에서

은 NSKeyedArchiver 각각 NSKeyedUnarchiver HTTPS의

+1

그리고 특히, @ MikeMayer67은 NSArchiver와 NSUnarchiver (NSCoder 수퍼 클래스를 통해 노출되지만 명시 적으로 구현되지 않음)에 키 방식 보관 방법을 사용하고 있습니다. [NSArchiver를 NSKeyedArchiver로 대체하고 NSUnarchiver를 NSKeyedUnarchiver로 대체하면 문제를 해결할 수 있습니다.] –

+1

언제나처럼 고맙습니다. @ItaiFerber – matt

+0

실제로 시작했는데 다른 문제가 있습니다. 키가없는 버전으로 전환하여 문제가 해결되는지 확인했습니다. 그리고 일이 아직 효과가 없을 때 그것을 되돌려 놓는 것을 잊었습니다. 키 코드가있는 버전으로 다시 전환해도 위 코드 샘플과 다른 문제가 있습니다. 추가 도움을 요청하기 전에 내가 그 밑으로 갈 수 있는지 알게 될 것입니다. – MikeMayer67