我想要繼承一些核心數據類。我有以下類:Objective C繼承
核心數據類:
@interface CDExplanatoryMaterial : NSManagedObject
@property (nonatomic, retain) NSString * document;
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * pageNumber;
@property (nonatomic, retain) NSNumber * realPageNumber;
@property (nonatomic, retain) NSString * url;
@end
業務邏輯類的協議:
@protocol BLDataClass <NSObject>
- (NSArray*)favouriteInGroups;
@property (nonatomic, readonly, retain) NSString* type;
@property (nonatomic, readonly, retain) NSArray* inFavouriteGroups;
- (void)addAddToFavouriteGroup: (NSString*) groupName;
- (void)removeFromFavouriteGroup: (NSString*) groupName;
- (void)addToHistory;
@end
接口BLExplanatoryMaterial:
@interface BLExplanatoryMaterial : CDExplanatoryMaterial <BLDataClass>
我得到的數據像這樣:
+ (NSMutableArray*) explanatoryMaterials {
NSMutableArray* results = [[NSMutableArray alloc] init];
for(CDExplanatoryMaterial *item in [Helper fetchDataObjectsOfType:@"CDExplanatoryMaterial"])
{
[results addObject: (BLExplanatoryMaterial*)item];
}
return results;
}
的輔助類看起來是這樣的:
@implementation Helper
+ (NSArray*) fetchDataObjectsOfType:(NSString *)type {
DataManager* manager = [DataManager sharedInstance];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:type inManagedObjectContext:manager.mainObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [manager.mainObjectContext executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
@end
這個問題我已經是在fetchDataObjectsOfType的fetchedObjects陣列和explanatoryMaterials結果數組只包含NSManagedObject對象。我希望fetchedObjects包含CDExplanatoryMaterial對象和結果以包含BLExplanatoryMaterial。我需要最終的結果是BLExplanatoryMaterial對象,或者我不能使用我的任何實例方法,那麼執行此操作的最佳方法是什麼?
感謝,
喬
編輯:
只是爲了澄清這是下面的代碼失敗,因爲expMat是NSManagedObject和不支持addToFavouriteGroup方法。
NSMutableArray* expMats = [Data explanatoryMaterials];
BLExplanatoryMaterial* expMat = (BLExplanatoryMaterial*) [expMats objectAtIndex:0];
[((BLExplanatoryMaterial*)expMat) addToFavouriteGroup:@"Test Group"]
有一件事我忘了提及的是,我原來的文章中的所有代碼都在靜態庫中。此編輯中發佈的代碼位於IOS應用程序項目中。我不確定這是否有所作爲。靜態庫中的所有類都標記爲公共。
我剛剛查看了配置 - >默認位,它說CDExplanatoryMaterial是CDExplanatoryMaterial實體的類。這看起來正確,我認爲?我會嘗試改變它。 – JoeS
更改班級沒有區別。 – JoeS
你可以重寫你的'NSManagedObject'子類中的'-awakeFromFetch',以查看它是否在從存儲中獲取對象時被調用? – Aderstedt