2015-12-15 104 views
2

我在更改項目名稱後遇到(我認爲)命名空間問題。以下是錯誤:爲OSX重命名Xcode項目後出現NSKeyedUnarchiver錯誤

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Apple.Document) for key (NS.objects); the class may be defined in source code or a library that is not linked'

有一個類似的問題解決here,但這指的是iOS版。對於Mac,只需更改display bundle name將不會更改在擴展塢圖標下顯示的名稱。

從「蘋果」到「梨花」, 改變我有問題,從CoreData越來越Document對象時,項目名稱後:

// Document 
    @objc(Document) 
    class Document: NSManagedObject { 
     @NSManaged var content: NSAttributedString 
    } 

文檔的content包含SpecialTextAttachment秒。

// SpecialTextAttachment 
    class SpecialTextAttachment: NSTextAttachment { 
     // 
    } 

當打印文檔的內容時,它給了我上面提到的錯誤。我將如何去轉換Apple.SpecialTextAttachmentPear.SpecialTextAttachment?看起來好像我需要在迭代Document之前對其進行轉換,因爲只涉及到Document類會導致崩潰。我也不能像建議here那樣簡單地爲這個班級創建另一個項目。

// in ViewController 
    func getDocuments() { 
     let fetchRequest = NSFetchRequest() 
     fetchRequest.entity = NSEntityDescription.entityForName("Document", inManagedObjectContext: managedObjectContext!) 
     fetchRequest.fetchBatchSize = 20 
     let docs = (try? managedObjectContext!.executeFetchRequest(fetchRequest)) as! [Document] 

    } 

回答

1

我有點放棄了這一點,並繼續與我決定重命名項目。

所以在我的類,它包含了所有從CoreData處理數據的方法的init(),我把下面的代碼:

class CoreDataHelper { 
    init() { 
     NSKeyedUnarchiver.setClass(SpecialTextAttachment.self, forClassName: "Apple.SpecialTextAttachment") 
    } 
} 

這將任何Apple.SpecialTextAttachment轉換爲Pear.SpecialTextAttachment任何即將到來的操作,在此SpecialTextAttachment可能會向上。

1

您可以在OS X上更改應用程序的顯示名稱,而無需更改軟件包名稱。該過程與iOS上的有所不同。

首先,如果你還沒有,你需要爲你的應用程序創建一個InfoPlist.strings文件。使用「OS X>資源>字符串文件」模板:

Creating InfoPlist.strings

然後定位InfoPlist.strings到基本定位:

Creating InfoPlist.strings Base localization

在字符串文件,指定你的新名稱CFBundleName鍵,如下所示:

CFBundleName = "Pear"; 

Setting CFBundleName in InfoPlist.strings

如果您已將CFBundleDisplayName密鑰設置爲Info.plist,則應將您的新名稱分配給CFBundleDisplayName,InfoPlist.strings。如果您尚未在Info.plist中設置CFBundleDisplayName密鑰,則不必將其設置爲InfoPlist.strings

這足以讓您的應用程序在其菜單欄和Finder中顯示其新名稱。但是,您的舊名稱仍會出現在硬編碼中,位於您的MainMenu.xibMain.storyboard的幾個位置。您可以通過選擇菜單欄中找到他們>查找>查找...(默認快捷鍵:⌘F)是這樣的:

finding Apple in MainMenu.xib

你需要這些改變爲「梨」。(從技術上講,您不必更改「Apple」菜單標題,因爲AppKit會在運行時將其更改爲InfoPlist.strings中的本地化字符串,但如果您只將「Apple」的所有實例更改爲「Pear」,則更簡單。)

如果您有其他語言的實際本地化版本,並且您希望將應用程序名稱本地化,則需要將InfoPlist.strings本地化爲您的每個本地化版本,並在其中每個版本中設置CFBundleName(也可能爲CFBundleDisplayName)。

+0

謝謝,但是設置CFBundleDisplayName並沒有改變任何東西。即使乾淨後。 –

+0

你不需要在Info.plist中設置CFBundleDisplayName(我不建議你這麼做)。我只說你需要在你的'InfoPlist.strings' **中設置**如果你有(因爲某種原因)將它設置在你的Info.plist中並且希望保持它在那裏設置。 –

0

發生這種情況的原因是,默認情況下,* .xcodeproj(project.pbxproj)在其他字段中使用$(TARGET_NAME)作爲產品名稱,使用$(PRODUCT_NAME)。例如,產品模塊名稱默認設置爲$(PRODUCT_NAME:c99extidentifier)。您只需要在Build Settings中將所有PRODUCT_NAME替換爲TARGET_NAME即可。