为什么继承必须从一个 NSObject 开始,然后在另一个类中使用继承?
想知道为什么我有一个来自 UIViewController 的子类“FatherClass”。 所以我想创建一个继承FatherClass(ChildClass: FatherClass)。
我不能这样做,我得到一个错误日志。 我在这里和 Google 搜索的所有示例都以 NSObject 作为父亲开头。
所以我的问题必须是这样吗?或者我哪里错了。
这是错误日志
#0 0x33d6c6f8 in CFStringGetCharacters ()
CoreFoundation`CFStringGetCharacters:
0x33d6c6f8: push.w {r8, r10}
谢谢
在此处编辑代码
父亲类.h
@class ChildClass;
@interface FatherClass : UIViewController <UITabBarControllerDelegate, UINavigationBarDelegate, UIPopoverControllerDelegate, MKMapViewDelegate, MKAnnotation>
{
//Some variables
}
@property (nonatomic, strong) ChildClass *sidebar_table_controller;
-(void) show_modal: (id) sender; // Thats the Method i want to call
@end
父亲类.m
#import "FatherClass.h"
#import "ChildClass.h"
@interface FatherClass ()
@end
@implementation FatherClass
@synthesize sidebar_table_controller;
// All inits here... DidiLoad, DidUnload, etc..
-(void) show_modal: (id) sender // Thats the Method!!!
{
// All initiate ChildClass an then.. present modal
[self presentModalViewController:self.sidebar_table_controller animated:YES];
NSLog(@"Clicked ?...%@", sender);
}
@end
现在是 child
子类.h
@interface ChildClass : FatherClass <UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
@end
子类.m
#import "ChildClass.h"
@interface ChildClass ()
@end
@implementation ChildClass
// All inits here... DidiLoad, DidUnload, etc..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here i want to call Father Method
[FatherClass show_modal:indexPath];
}
@end
当我将 ChildClass : UIViewController 更改为 ChildClass : FatherClass 时,我的应用程序崩溃了。
最佳答案
您可以创建任何类型的对象:具有 NSObject 父亲的对象或没有任何父亲的新对象。 那么问题来了:为什么要用 NSObject 作为父亲呢?因为它实现了创建健壮类所需的每个方法,例如 init、dealloc 等等。
因此您可以创建自己的层次结构,但您必须重新发明轮子重写每个方法来管理对象。
关于objective-c - 为什么继承需要是 "subclass"NSObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301272/