2014-09-30 33 views
0

我正在關注Core Data上的Tim Roadley書籍教程。我試圖在swift中做一個遷移管理器,但我得到這個錯誤:在調用中額外的參數'forStoreMetadata'。 這是因爲這種方法是這樣定義的:mergedModelFromBundles在Swift中不接受可選參數

NSManagedObjectModel.mergedModelFromBundles(bundles: [AnyObject], 
forStoreMetadata: [NSObject : AnyObject]) 

我認爲這是錯誤的,因爲它缺少對包的選擇這樣一個定義:

NSManagedObjectModel.mergedModelFromBundles(bundles: [AnyObject]?) 

我如何工作的這個局面?我正在試圖做的是:被定義

var sourceModel = NSManagedObjectModel.mergedModelFromBundles(nil, forStoreMetadata: sourceMetadata) 

回答

0

要回答我的問題:

爲了在迅速下同行爲:

NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:sourceMetadata]; 

這是需要申報[NSBundle.mainBundle()],而不是零,因爲它沒有收到可選。這是正確的方法,因爲在NSBundle()文檔中可以看到nil也被看作主包。

let sourceModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle.mainBundle()], forStoreMetadata: sourceMetadata) 

我認爲這是一個蘋果軟件缺陷,因爲在SWIFT()有很多的功能,這接受零,而不是NSBundle.mainBundle。

0

兩個類方法如下(在NSManagedObjectModel命令點擊跳轉至其頭):

class func mergedModelFromBundles(bundles: [AnyObject], forStoreMetadata metadata: [NSObject : AnyObject]) -> NSManagedObjectModel? 

和:

class func mergedModelFromBundles(bundles: [AnyObject]?) -> NSManagedObjectModel! 

請注意,最後一個bundles被聲明爲可選項,而在第一個方法中不是。這意味着當你通過nilmergedModelFromBundles時,編譯器假設你想要一個接受nil,然後抱怨該方法沒有第二個參數。

因此,如果您要傳遞nil,請使用單參數方法;如果您要指定metadata,請提供實際的[AnyObject]

從文檔

Sometimes a model—particularly one in a framework—may be used in different situations, and you may want to specify different sets of entities to be used in different situations. There might, for example, be certain entities that should only be available if a user has administrative privileges. To support this requirement, a model may have more than one configuration. Each configuration is named, and has an associated set of entities. The sets may overlap. You establish configurations programmatically using setEntities:forConfiguration: or using the Xcode design tool, and retrieve the entities for a given configuration name using entitiesForConfiguration:.

和:

class func mergedModelFromBundles(_ bundles: [AnyObject]!) -> NSManagedObjectModel! 

bundles是:

An array of instances of NSBundle to search. If you specify nil, then the main bundle is searched.

併爲:

class func mergedModelFromBundles(bundles: [AnyObject], forStoreMetadata metadata: [NSObject : AnyObject]) -> NSManagedObjectModel? 

Returns the managed object model used to create the store for the specified metadata. This method is a companion to the mergedModelFromBundles: method; in this case, the framework uses the version information stored in the metadata for a store to locate the models/entities used to create the store in the available bundles, and return the model. If the model for the store cannot be found, this method will return nil.

+0

但是,我怎樣才能將零傳遞到捆綁包,並仍然添加元數據? – Supsup 2014-10-04 10:42:17

+0

我已將更多信息添加到我的答案中。這回答了你的問題了嗎? – milos 2014-10-04 13:22:35

+0

感謝您的詳細解答。我已經理解你說了什麼,但我仍然有同樣的問題,如果我想要這樣做: NSManagedObjectModel * sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:sourceMetadata]; 在Swift中,它不可能,但在客觀的C它是。 – Supsup 2014-10-05 01:49:25