开发者

Why This Class <Not Responding>

开发者 https://www.devze.com 2023-01-23 17:55 出处:网络
Hi i dont know why i call a function in \"msg\" class it has no respond:( this is the \"msg\" class: msg.h :

Hi i dont know why i call a function in "msg" class it has no respond:(

this is the "msg" class:

msg.h :

#import <UIKit/UIKit.h>


@interface msg : NSObject {

}

-(void) Print;

@end

msg.m :

#import "msg.h"


@implementation msg

-开发者_如何学Python(void) Print {
 NSLog(@"Hello World");
}

@end

viewController.h :

#import <UIKit/UIKit.h>
#import "msg.h"

@class msg;
@interface ClassMod4ViewController : UIViewController {

 msg *object;

}

@property (nonatomic,retain) msg *object;

@end

viewController.m :

#import "ClassMod4ViewController.h"

@implementation ClassMod4ViewController
@synthesize object;

- (void)viewDidLoad {

 [object Print];

    [super viewDidLoad];
}

Thanks


Was the object initialized in the designated initializer (usually initWithNibName:bundle: for UIViewController subclasses) prior to viewDidLoad being invoked?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    // ...
    object = [[Msg alloc] init];
    // ...
    return self;
}

If the object has not been initialized, then it's nil. Remember that it's okay to send nil objects messages in Objective-C (nothing will happen as a result).


have you tried:

- (void)viewDidLoad {
 assert(object);    
 [object Print];
 [super viewDidLoad];
}

?

if the assertion fails, then the pointer to object is 0 (or nil). you must create the object before you use it. this is typically performed in the initializer (in your case, of the class that contains the instance of object):

- (id)init {
    self = [super init];
    if (0 != self) {
        object = [[msg alloc] init];
    }
    return self;
}

but sometimes you'll want to create the object later on.

if you are initializing it, then you may want to set breakpoints where the object is accessed in order to determine where the object is set to 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号