简体   繁体   中英

iOS description method not called

I've overridden the description method of an object I created, quite simple. This object is a subclass of NSMutableURLRequest .

- (NSString *)description
{
    return [[NSString alloc] initWithData:self.HTTPBody encoding:NSUTF8StringEncoding];
}

I also put - (NSString *)description; in the .h

But it is not called when I NSLog the object. It is not a NSManagedObject . Even the debugger won't step into "description" if I only call myObject.description; . I am calling the method precisely on an instance of my object, not just a NSMutableURLRequest .

EDIT: I instantiate the object like this:

MYRequest *myRequest = [MYRequest requestWithFilter:myFilter];

NSLog(@"%@", myRequest);

And here is the factory method:

@interface MYRequest : NSMutableURLRequest
+ (instancetype)requestWithFilter:(NSString *)filter;


@implementation MYRequest
+ (instancetype)requestWithFilter:(NSString *)filter
{
    // some config
    MYRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
    // some more config
    return request;
}

What the heck?

您没有子类的实例,只有一个普通的NSMutableURLRequest

NSMutableURLRequest or NSURLRequest classes doesn't have - (NSString *)description method declared.

You are accessing NSObject 's method - (NSString *)description .

See this

You should declare your own description method in your custom class and call it like:

NSString *myObjectDescription = [myCustomObject description];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM